What if your program could patiently wait and keep trying until it gets exactly what it needs?
Why while True pattern in Python? - Purpose & Use Cases
Imagine you want to keep asking your friend for their favorite color until they give you a valid answer. You try to remember to ask again and again manually, but it's easy to forget or get stuck.
Manually repeating the same question over and over is tiring and easy to mess up. You might forget to ask again, or your program might stop too soon or run forever without control.
The while True pattern lets your program keep doing something again and again until you tell it to stop. It's like having a smart helper who keeps asking until the answer is right.
answer = input('Favorite color? ') if answer not in ['red', 'blue', 'green']: answer = input('Please try again: ')
while True: answer = input('Favorite color? ') if answer in ['red', 'blue', 'green']: break
This pattern makes your programs flexible and interactive, letting them wait patiently for the right input or condition before moving on.
Think about an ATM machine that keeps asking for your PIN until you enter the correct one. The while True pattern helps build that kind of reliable loop.
Repeats actions easily: Keeps running code until you say stop.
Controls flow: Lets you wait for the right moment or input.
Prevents mistakes: Avoids forgetting to repeat or stopping too soon.