Python While Loop Exercises With Five Beginner Problems
You understand for loops, then a while loop runs forever and your laptop fan starts screaming. Or it never runs at all and you cannot see why.

You understand for loops, then a while loop runs forever and your laptop fan starts screaming. Or it never runs at all and you cannot see why. These Python while loop exercises are built to fix exactly that, with five short problems that each teach one thing a while loop does that a for loop does not.
I use this set with new members at our club in a single 60 minute session. Each problem takes 5 to 15 minutes. Do them in order, type every line yourself, and resist copying from a search result. The struggle is the point.
What makes a while loop different
A for loop walks through something that already exists: a list, a string, a range of numbers. You know how many steps it will take before it starts. A while loop is different. It keeps going as long as a condition stays true, and it checks that condition before every pass.
That means every while loop needs three parts:
- A starting value set before the loop, such as count = 0.
- A condition on the while line, such as count less than 5.
- A change inside the loop that eventually makes the condition false, such as adding 1 to count.
Forget the third part and the loop never ends. Get the first part wrong and it may never start. Almost every while loop bug a beginner hits comes from one of these three. If loops in general still feel abstract, our guide to Python loops explained with things you already do is a gentler place to begin.
One safety tip before you start: if a loop runs forever, press Ctrl and C together in the terminal to stop it. Nothing is broken. You just found a bug.
Five Python while loop exercises
Exercise 1: the countdown
Write a program that starts at 10 and prints each number down to 1, then prints "Liftoff!". Use a while loop, not a for loop.
What it teaches: the three parts in their simplest form. Your starting value is 10, the condition is that the number is greater than 0, and the change is subtracting 1 each pass. Try deleting the subtraction line once on purpose and watch what happens, then stop it with Ctrl and C. Seeing an infinite loop on purpose makes the next one far less scary.
Exercise 2: the password prompt
Ask the user to type a password. Keep asking until they type "dragon". When they get it right, print "Welcome in".
What it teaches: this is the problem for loops cannot solve cleanly, because you have no idea how many tries the user will need. Set the guess to an empty string before the loop so the condition has something to check on the first pass. Students often forget that step and get a NameError on the while line.
Stretch goal: allow only three attempts. Now your condition has two parts joined with "and": the guess is wrong and attempts are fewer than 3. After the loop, check which part ended it and print either the welcome or "Locked out".
Exercise 3: running total until zero
Ask the user for numbers one at a time. Add each to a running total. When they type 0, stop and print the total and how many numbers they entered, not counting the zero.
What it teaches: the "sentinel value" pattern, where a special input means stop. You will need to convert each input with int() and keep two variables, a total and a counter. Test it with 4, 6, 10 and 0. You should get a total of 20 from 3 numbers. If you get 4 numbers, your counter is counting the zero.
Exercise 4: doubling savings
You put 1 dollar in a jar on day one and double the amount every day. Print the day number and the amount, and stop on the first day the jar holds more than 1,000 dollars.
What it teaches: loops where the stopping point comes from a calculation, not a fixed count. The answer is day 11, when the amount reaches 1,024. If you print day 10 and 512, check if your condition uses "less than" or "less than or equal to" and if you double before or after printing. Small order changes shift the answer by one, which is the off-by-one error every programmer meets weekly.
Exercise 5: the number guessing game
Import random and pick a secret number from 1 to 50. Let the player guess. After each wrong guess, print "Too high" or "Too low". When they get it, print how many guesses it took.
What it teaches: everything above in one program. Starting value, a condition that depends on input, a counter, and if else inside the loop body. If you want more practice on that last part, try the Python if else exercises with six beginner problems first. A good player should win in 6 guesses or fewer by always guessing the middle of what is left, which is a nice hint toward binary search later.
Why you should not reach for while True first
Plenty of tutorials teach "while True" with a break inside as the standard way to write input loops. It works, and experienced programmers use it all the time. I still ask beginners to avoid it for their first few weeks.
The reason is that while True hides the condition. The whole point of these exercises is learning to say, in one line, what has to be true for the loop to keep running. With while True, the stopping rule is buried somewhere in the body, sometimes in two or three different break statements. When the loop misbehaves, a beginner has to hunt for it.
Write each exercise above with a real condition first. Once you can do that without thinking, rewrite exercise 2 with while True and break, and compare the two versions. You will understand why both exist, and you will choose on purpose instead of by habit.
How to check your answers without peeking
Before looking anything up, try these steps:
- Add a print line at the very top of the loop body that shows every variable. Watching the values change on each pass catches most bugs in under a minute.
- Trace the loop by hand on paper for the first three passes. Write the value of each variable in a column.
- Test the edges. What happens if the user types 0 first in exercise 3? What if they guess right on the first try in exercise 5?
If you are still stuck after 15 minutes, our guide on getting unstuck on a practice problem without peeking walks through the next steps.
Frequently asked questions
When should I use a while loop instead of a for loop in Python?
Use a while loop when you do not know in advance how many times it needs to run, such as waiting for correct input or running until a value crosses a limit. Use a for loop when you are stepping through a list, string or range of known length. If you find yourself managing a counter by hand in a while loop, a for loop is often cleaner.
How do I stop an infinite while loop in Python?
Press Ctrl and C in the terminal to interrupt the program. Then check that something inside the loop changes the variable in your condition, and that the change moves it toward making the condition false. Printing the variable on each pass usually shows the problem quickly.
What is a good first while loop exercise for beginners?
A countdown from 10 to 1 is the best starting point because it uses all three parts of a while loop with nothing else in the way. After that, a password prompt that repeats until the right answer shows why while loops exist at all.
More in Python and Practice
Python and Practice
Python Enumerate and Zip Practice With Five Exercises
A student at our Tuesday club spent eleven minutes last month hunting a bug in four lines of code.
Python and Practice
Python Set Practice With Five Beginner Exercises
A learner in my Thursday club brought me 34 lines of code that found the duplicate names in a sign-in sheet.
Python and Practice
Python List Comprehension Practice With Five Exercises
A student in my Tuesday group handed me a line of code last spring that was 118 characters long, had two for clauses and an if...