Challenge - 5 Problems
While Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple counter-based while loop
What is the output of this Python code?
Python
count = 0 while count < 3: print(count) count += 1
Attempts:
2 left
💡 Hint
Remember, the loop runs while count is less than 3, starting from 0.
✗ Incorrect
The loop starts at 0 and prints count before increasing it by 1. It stops before count reaches 3, so it prints 0, 1, and 2.
❓ Predict Output
intermediate2:00remaining
Value of counter after loop ends
What is the value of variable 'counter' after this code runs?
Python
counter = 5 while counter > 0: counter -= 2
Attempts:
2 left
💡 Hint
Check how counter changes each loop iteration until the condition fails.
✗ Incorrect
Starting at 5, counter decreases by 2 each time: 5 -> 3 -> 1 -> -1. The loop stops when counter is no longer > 0, so final value is -1.
🔧 Debug
advanced2:00remaining
Identify the error in this counter-based while loop
What error does this code produce when run?
Python
i = 0 while i < 5: print(i) i += 1
Attempts:
2 left
💡 Hint
Check the syntax of the while statement line.
✗ Incorrect
The while statement is missing a colon at the end, causing a SyntaxError.
❓ Predict Output
advanced2:00remaining
Output of a while loop with break and counter
What is the output of this code?
Python
count = 0 while True: if count == 3: break print(count) count += 1
Attempts:
2 left
💡 Hint
The loop breaks when count reaches 3, so it does not print 3.
✗ Incorrect
The loop prints count starting at 0 and stops before printing 3 because of the break.
❓ Predict Output
expert3:00remaining
Final value of counter with complex condition
What is the final value of 'counter' after this code runs?
Python
counter = 10 while counter > 0: counter -= 3 if counter == 4: counter += 1
Attempts:
2 left
💡 Hint
Track the counter value each loop iteration carefully.
✗ Incorrect
The counter decreases by 3 each time: 10 -> 7 -> 4. When counter is 4, it increases by 1 to 5, then continues: 5 -> 2 -> -1. The loop stops when counter is no longer > 0, so final value is -1 after last decrement.