Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does the while True loop do in Python?
It creates an infinite loop that keeps running until it is stopped by a break statement or an external interruption.
Click to reveal answer
beginner
How do you stop a while True loop inside the loop?
You use the break statement to exit the loop when a certain condition is met.
Click to reveal answer
intermediate
Why might you use a while True loop instead of a while loop with a condition?
Using while True lets you write the exit condition inside the loop body, which can make the code easier to read and handle complex exit conditions.
Click to reveal answer
beginner
What is a common real-life example where while True is useful?
When waiting for user input repeatedly until they enter a valid response, like asking for a password until it is correct.
Click to reveal answer
beginner
What happens if you forget to include a break in a while True loop?
The program will run forever, causing an infinite loop that can freeze or crash your program.
Click to reveal answer
What keyword is used to exit a while True loop?
Abreak
Bcontinue
Cexit
Dstop
✗ Incorrect
The break statement immediately stops the loop and exits it.
What will happen if a while True loop has no break?
AThe loop runs forever
BThe loop runs once
CThe loop never runs
DThe loop runs twice
✗ Incorrect
Without a break, the while True loop never stops on its own.
Which of these is a good use case for while True?
ANone of the above
BRunning a loop a fixed number of times
CRepeatedly asking user input until valid
DLooping through a list
✗ Incorrect
while True is great for loops that run until a condition inside the loop is met, like user input validation.
What does the continue statement do inside a while True loop?
AExits the loop
BSkips to the next loop iteration
CPauses the loop
DEnds the program
✗ Incorrect
continue skips the rest of the current loop and starts the next iteration.
Which of these is NOT true about while True loops?
AThey always need a <code>break</code> to stop
BThey run until stopped
CThey can be used for infinite loops
DThey automatically stop after 10 iterations
✗ Incorrect
while True loops do not stop automatically after any number of iterations.
Explain how a while True loop works and how to safely stop it.
Think about what keeps the loop running and what stops it.
You got /3 concepts.
Describe a real-life scenario where using a while True loop is helpful.
Imagine asking someone for input until they give a correct answer.
You got /4 concepts.
Practice
(1/5)
1.
What does the while True loop do in Python?
easy
A. It creates a loop that runs forever unless stopped by break.
B. It runs the loop only once.
C. It runs the loop a fixed number of times.
D. It causes a syntax error.
Solution
Step 1: Understand the meaning of while True
The condition True is always true, so the loop will keep running forever.
Step 2: Recognize how to stop the loop
To stop this infinite loop, a break statement is used inside the loop when a condition is met.
Final Answer:
It creates a loop that runs forever unless stopped by break. -> Option A
Quick Check:
while True = infinite loop until break [OK]
Hint: Remember: while True loops forever until break stops it [OK]
Common Mistakes:
Thinking it runs only once
Assuming it runs a fixed number of times
Believing it causes a syntax error
2.
Which of the following is the correct syntax to stop a while True loop when a variable count reaches 5?
count = 0
while True:
count += 1
?
easy
A. if count == 5: break
B. if count = 5: break
C. if count == 5: continue
D. if count > 5: stop
Solution
Step 1: Identify correct comparison operator
Use == to compare values, so if count == 5 is correct.
Step 2: Use break to exit the loop
The break statement stops the loop immediately when the condition is true.
Final Answer:
if count == 5: break -> Option A
Quick Check:
Use == and break to stop loop [OK]
Hint: Use double equals (==) for comparison and break to stop [OK]
Common Mistakes:
Using single equals (=) instead of double equals (==)
Using continue instead of break
Using undefined commands like stop
3.
What will be the output of the following code?
i = 0
while True:
i += 2
if i > 6:
break
print(i)
medium
A. 0
B. 6
C. 4
D. 8
Solution
Step 1: Trace the loop increments
i starts at 0, then increases by 2 each loop: 2, 4, 6, 8.
Step 2: Check the break condition
The loop breaks when i > 6, which happens when i becomes 8.
Final Answer:
6 -> Option B
Quick Check:
Loop stops at i=8 because 8 > 6, but the printed value is the last before break, which is 6 [OK]
Hint: Add increments step-by-step until break condition met [OK]
Common Mistakes:
Stopping at i=6 instead of i=8
Printing before break
Confusing break condition with >= instead of >
4.
Find the error in this code snippet:
count = 0
while True
count += 1
if count == 3:
break
print(count)
medium
A. Wrong comparison operator in if count == 3
B. Incorrect indentation of count += 1
C. Missing break statement
D. Missing colon after while True
Solution
Step 1: Check syntax of while statement
Python requires a colon (:) after while True to start the loop block.
Step 2: Verify other parts
Indentation and comparison operator are correct; break is present.
Final Answer:
Missing colon after while True -> Option D
Quick Check:
Loops need colon after condition [OK]
Hint: Look for missing colons after loop or if statements [OK]
Common Mistakes:
Forgetting colon after while
Misaligning indentation
Confusing = and == in conditions
5.
You want to write a program that keeps asking the user to enter a number until they enter a negative number. Which code snippet correctly uses while True to do this?
hard
A. while num >= 0:
num = int(input('Enter number: '))
print(f'You entered {num}')
B. while True:
num = int(input('Enter number: '))
if num > 0:
break
print(f'You entered {num}')
C. while True:
num = int(input('Enter number: '))
if num < 0:
break
print(f'You entered {num}')
D. while True:
num = input('Enter number: ')
if num < 0:
break
Solution
Step 1: Check loop condition and input
while True:
num = int(input('Enter number: '))
if num < 0:
break
print(f'You entered {num}') uses while True and asks for input inside the loop, converting it to int.
Step 2: Verify break condition and output
It breaks when the number is negative (< 0) and prints the number otherwise.
Final Answer:
while True:
num = int(input('Enter number: '))
if num < 0:
break
print(f'You entered {num}') -> Option C
Quick Check:
Break on negative input inside infinite loop [OK]
Hint: Break loop when negative number entered, print otherwise [OK]
Common Mistakes:
Breaking on wrong condition (positive instead of negative)