Challenge - 5 Problems
While–else Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of while–else with break
What is the output of this Python code?
Python
i = 0 while i < 3: if i == 1: break print(i) i += 1 else: print('Done')
Attempts:
2 left
💡 Hint
Remember that the else block runs only if the loop completes without break.
✗ Incorrect
The loop breaks when i == 1, so the else block does not run. Only 0 is printed.
❓ Predict Output
intermediate2:00remaining
While–else without break
What will this code print?
Python
count = 0 while count < 2: print(count) count += 1 else: print('Finished')
Attempts:
2 left
💡 Hint
The else block runs only if the loop ends normally without break.
✗ Incorrect
The loop runs twice printing 0 and 1, then else prints 'Finished'.
❓ Predict Output
advanced2:00remaining
While–else with continue and break
What is the output of this code?
Python
n = 0 while n < 4: n += 1 if n == 2: continue if n == 3: break print(n) else: print('Loop ended')
Attempts:
2 left
💡 Hint
Check how continue skips printing and break stops the loop.
✗ Incorrect
When n==2, continue skips print. When n==3, break stops loop before print. Only 1 is printed.
❓ Predict Output
advanced2:00remaining
While–else with nested loops and break
What does this code print?
Python
x = 0 while x < 3: y = 0 while y < 3: if x == y: break print(f'{x},{y}') y += 1 else: print('Inner done') x += 1 else: print('Outer done')
Attempts:
2 left
💡 Hint
Remember break exits the inner loop and else runs only if no break.
✗ Incorrect
Inner loop breaks when x == y, so inner else never runs. Outer loop ends normally, so outer else runs.
🧠 Conceptual
expert2:00remaining
Understanding while–else behavior
Which statement about the while–else construct in Python is correct?
Attempts:
2 left
💡 Hint
Think about when else runs in loops with break.
✗ Incorrect
The else block runs only if the loop finishes normally without break.