Challenge - 5 Problems
While Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a while loop counting down
What is the output of this Python code?
Python
count = 3 while count > 0: print(count) count -= 1 print("Done")
Attempts:
2 left
💡 Hint
Think about how the while loop runs as long as the condition is true.
✗ Incorrect
The while loop prints the current count starting from 3 down to 1, then prints 'Done' after the loop ends.
🧠 Conceptual
intermediate2:00remaining
Why use a while loop instead of a for loop?
Which situation best shows why a while loop is needed instead of a for loop?
Attempts:
2 left
💡 Hint
Think about loops that run until something changes, not a set count.
✗ Incorrect
A while loop is best when you don't know how many times you need to repeat, only that you continue until a condition is false.
❓ Predict Output
advanced2:00remaining
Output of a while loop with break
What is the output of this code?
Python
i = 0 while True: if i == 3: break print(i) i += 1 print('Stopped')
Attempts:
2 left
💡 Hint
Look at when the break stops the loop.
✗ Incorrect
The loop prints 0,1,2 then breaks when i equals 3, then prints 'Stopped'.
❓ Predict Output
advanced2:00remaining
What error does this code raise?
What error will this code raise?
Python
count = 5 while count > 0: print(count) count -= 1
Attempts:
2 left
💡 Hint
Check the syntax of the while statement.
✗ Incorrect
The while statement is missing a colon at the end, causing a SyntaxError.
🧠 Conceptual
expert3:00remaining
Why is a while loop needed for user input validation?
Why is a while loop the best choice to keep asking a user for input until they enter a valid number?
Attempts:
2 left
💡 Hint
Think about repeating until a condition is met, not a fixed count.
✗ Incorrect
User input validation requires repeating until the input is valid, which can take an unknown number of tries, so a while loop is ideal.