Challenge - 5 Problems
For Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested for loops with break
What is the output of this Python code?
Python
result = [] for i in range(3): for j in range(3): if j == 1: break result.append((i, j)) print(result)
Attempts:
2 left
💡 Hint
Remember that 'break' stops the inner loop only.
✗ Incorrect
The inner loop breaks when j == 1, so only j = 0 is appended for each i. The outer loop continues for i = 0, 1, 2.
❓ Predict Output
intermediate1:30remaining
For loop with else clause output
What will be printed by this code?
Python
for num in range(3): print(num) else: print('Done')
Attempts:
2 left
💡 Hint
The else block runs if the loop completes normally.
✗ Incorrect
The else block runs after the loop finishes all iterations without a break.
❓ Predict Output
advanced1:30remaining
Effect of modifying loop variable inside for loop
What is the output of this code?
Python
nums = [1, 2, 3] for n in nums: n += 10 print(nums)
Attempts:
2 left
💡 Hint
Changing the loop variable does not change the list elements.
✗ Incorrect
The variable n is a copy of each element. Modifying n does not affect the original list.
❓ Predict Output
advanced1:30remaining
Loop with continue and else behavior
What will this code print?
Python
for i in range(3): if i == 1: continue print(i) else: print('Finished')
Attempts:
2 left
💡 Hint
Continue skips the current iteration but does not stop the loop.
✗ Incorrect
The loop skips printing 1 but continues. The else runs after the loop ends.
🧠 Conceptual
expert1:30remaining
Understanding for loop iteration and variable scope
Consider this code snippet. What is the value of
i after the loop finishes?Python
for i in range(5): pass print(i)
Attempts:
2 left
💡 Hint
The loop variable keeps the last value it had in the loop.
✗ Incorrect
After the loop ends, i holds the last value from range(5), which is 4.