0
0
Pythonprogramming~20 mins

while True pattern in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
While True Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
A
0
1
2
3
4
B
0
1
2
3
C
1
2
3
D
0
1
2
Attempts:
2 left
💡 Hint
The loop stops when count reaches 3, so it prints before breaking.
Predict Output
intermediate
2: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)
A
1
2
3
B
1
3
C
1
3
4
D
2
3
Attempts:
2 left
💡 Hint
The continue skips printing when i is 2.
🔧 Debug
advanced
2:00remaining
Identify the error in this while True loop
What error does this code raise when run?
Python
while True
    print("Hello")
    break
ASyntaxError
BIndentationError
CNameError
DNo error, prints Hello once
Attempts:
2 left
💡 Hint
Check the syntax of the while statement.
Predict Output
advanced
2: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
A
0,0
1,0
2,0
B
0,0
0,1
0,2
1,0
1,1
2,0
2,1
C
0,0
0,1
1,0
1,1
2,0
2,1
D
0,0
0,1
1,0
1,1
2,0
Attempts:
2 left
💡 Hint
Inner loop runs twice for each outer loop iteration.
🧠 Conceptual
expert
2: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?
ATo handle complex exit conditions inside the loop body more clearly
BTo create an infinite loop that never stops
CBecause while True loops run faster than while with conditions
DTo avoid indentation errors
Attempts:
2 left
💡 Hint
Think about readability and control inside the loop.