Conditions & Loops · Question 18 · Hard
The loop variable is changed inside the body of a for loop. What does this print after the loop?
total = 0
for i in range(3):
total += i
i += 10
print(i, total)
- 2 3
- 12 33
- 12 3
- 32 3
Answer and explanation
Answer: C. 12 3
Explanation: At the start of every iteration i is reassigned the next value from range(3), so changing i inside the body does not affect the loop: total is 0 + 1 + 2 = 3. In the last iteration i is 2 and then becomes 12, and that value remains after the loop.
Previous: A while loop with an else clause tests whether 91 has a small divisor. What… · Next: Both conditions of this if-elif contain a division by x, and x is 0. What is…
Practise conditions & loops
Try a fresh set of conditions & loops questions with an explanation after every answer, free and without signing up: Conditions & Loops practice. Or sit a full placement mock test.
More conditions & loops questions
- A while loop halves even numbers and applies 3n + 1 to odd numbers until n…
- break appears inside the inner of two nested loops. Which pairs are collected?
- A while loop with an else clause tests whether 91 has a small divisor. What…
- Both conditions of this if-elif contain a division by x, and x is 0. What is…
- continue comes after the increment inside a while loop. What list is built?
More python practice
Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge