Challenge - 5 Problems
While True Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple while True loop with break
What is the output of this code?
Python
count = 0 while True: print(count) count += 1 if count == 3: break
Attempts:
2 left
💡 Hint
The loop stops when count reaches 3, so it prints before breaking.
✗ Incorrect
The loop starts at 0 and prints count each time. When count becomes 3, the break stops the loop before printing 3.
❓ Predict Output
intermediate2:00remaining
While True loop with continue and break
What is the output of this code?
Python
i = 0 while True: i += 1 if i == 2: continue if i == 4: break print(i)
Attempts:
2 left
💡 Hint
The continue skips printing when i is 2.
✗ Incorrect
When i is 2, continue skips the print. The loop breaks at i == 4, so 4 is not printed. Only 1 and 3 are printed.
🔧 Debug
advanced2:00remaining
Identify the error in this while True loop
What error does this code raise when run?
Python
while True print("Hello") break
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.
❓ Predict Output
advanced2:00remaining
Output of nested while True loops with break
What is the output of this code?
Python
x = 0 while True: y = 0 while True: print(f"{x},{y}") y += 1 if y == 2: break x += 1 if x == 3: break
Attempts:
2 left
💡 Hint
Inner loop runs twice for each outer loop iteration.
✗ Incorrect
The inner loop prints y from 0 to 1, then breaks. The outer loop runs x from 0 to 2, then breaks.
🧠 Conceptual
expert2:00remaining
Why use while True with break instead of a condition?
Which is the best reason to use a while True loop with a break inside instead of a while loop with a condition?
Attempts:
2 left
💡 Hint
Think about readability and control inside the loop.
✗ Incorrect
Using while True with break allows placing multiple exit points inside the loop, making complex conditions easier to manage.