The while True pattern lets a program keep doing something over and over until you tell it to stop. It is useful when you don't know how many times you need to repeat a task.
while True pattern in Python
Start learning this pattern below
Jump into concepts and practice - no test required
while True: # code to repeat if some_condition: break
The loop runs forever because True is always true.
You use break inside the loop to stop it when needed.
while True: print("Hello") break
while True: answer = input("Type 'exit' to stop: ") if answer == 'exit': break print(f"You typed: {answer}")
This program keeps asking the user to enter a number. If the user types 'stop', it says goodbye and ends. If the user types a number, it shows it back. If the input is not a number, it asks again.
while True: number = input("Enter a number (or 'stop' to end): ") if number == 'stop': print("Goodbye!") break if number.isdigit(): print(f"You entered: {number}") else: print("That's not a number. Try again.")
Always make sure there is a break inside the while True loop to avoid an infinite loop that never stops.
You can use continue to skip to the next loop cycle if needed.
while True creates a loop that runs forever until stopped.
Use break inside the loop to stop it when a condition is met.
This pattern is great when you don't know how many times you need to repeat something.
Practice
What does the while True loop do in Python?
Solution
Step 1: Understand the meaning of
The conditionwhile TrueTrueis always true, so the loop will keep running forever.Step 2: Recognize how to stop the loop
To stop this infinite loop, abreakstatement is used inside the loop when a condition is met.Final Answer:
It creates a loop that runs forever unless stopped bybreak. -> Option AQuick Check:
while True= infinite loop untilbreak[OK]
while True loops forever until break stops it [OK]- Thinking it runs only once
- Assuming it runs a fixed number of times
- Believing it causes a syntax error
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
?Solution
Step 1: Identify correct comparison operator
Use==to compare values, soif count == 5is correct.Step 2: Use
Thebreakto exit the loopbreakstatement stops the loop immediately when the condition is true.Final Answer:
if count == 5: break -> Option AQuick Check:
Use==andbreakto stop loop [OK]
- Using single equals (=) instead of double equals (==)
- Using continue instead of break
- Using undefined commands like stop
What will be the output of the following code?
i = 0
while True:
i += 2
if i > 6:
break
print(i)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 BQuick Check:
Loop stops at i=8 because 8 > 6, but the printed value is the last before break, which is 6 [OK]
- Stopping at i=6 instead of i=8
- Printing before break
- Confusing break condition with >= instead of >
Find the error in this code snippet:
count = 0
while True
count += 1
if count == 3:
break
print(count)Solution
Step 1: Check syntax of
Python requires a colon (:) afterwhilestatementwhile Trueto start the loop block.Step 2: Verify other parts
Indentation and comparison operator are correct;breakis present.Final Answer:
Missing colon afterwhile True-> Option DQuick Check:
Loops need colon after condition [OK]
- Forgetting colon after while
- Misaligning indentation
- Confusing = and == in conditions
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?
Solution
Step 1: Check loop condition and input
while True: num = int(input('Enter number: ')) if num < 0: break print(f'You entered {num}') useswhile Trueand 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 CQuick Check:
Break on negative input inside infinite loop [OK]
- Breaking on wrong condition (positive instead of negative)
- Not converting input to int
- Missing print statement or break
