Paul's Python CoursePPC

Why does my while loop never stop?

, 3 minutes to read

A while loop goes round for as long as its test is True, so a loop that never stops has a test that never becomes False. The usual cause is that nothing inside the loop changes the variable the test is checking, so look for the line in the loop that should update it.

The variable never changes

Here's a loop that's meant to run three times.

tries = 0
while tries < 3:
    print(f"Guess number {tries}")
print("Out of guesses.")

tries starts at 0, and nothing in the loop ever adds to it, so tries < 3 is true every time Python checks and it prints Guess number 0 over and over. Python doesn't change a variable just because it's in the test. You have to do that yourself, with a line like tries = tries + 1 inside the loop.

It has to be indented with the rest of the loop. If it isn't, it sits after the loop, and the loop never gets to it. A line that changes the variable the wrong way does the same damage, such as a countdown that says countdown = countdown + 1 when it meant - 1.

On this site, the editor stops a runaway program after 2000 lines of output and says "Stopped after 2000 lines of output, which is usually a loop that never ends." A loop that doesn't print anything just keeps running until you press Stop. In a terminal on your own computer, Ctrl+C does the same job.

A string that never equals a number

This one catches people out in guessing games. Run the example and type 3, then 7. Even though 7 is the secret number, it keeps asking. Press Stop, then change line 4 to guess = int(input("Guess? ")) and run it again. This time 7 gets you Got it.

Run it

main.py

When it asks, try typing "3", "7".

secret = 7
guess = 0
while guess != secret:
    guess = input("Guess? ")
print("Got it.")

Output

Press Run and what your program prints shows up here.

The loop does change guess each time round, but input() always gives back text. Typing 7 gives the string "7", and the string "7" is never equal to the number 7, so guess != secret stays true however many times you get it right. Converting with int() makes both sides numbers, and then they can match.

When you can't see why a loop won't stop, put a print() as the first line inside it that shows the variable from the test. Watching the value go round usually shows the problem within a few lines.

A small exercise

This loop is meant to show the fuel going down and then print Empty., but it never stops. Work out why, and fix it by changing one line.

fuel = 10
while fuel != 0:
    print(f"Fuel left: {fuel}")
    fuel = fuel - 3
print("Empty.")
Show an answer

The fuel goes 10, 7, 4, 1, then -2. It jumps straight past 0, so fuel != 0 is never false. Change the test so it stops at 0 or anything below it.

fuel = 10
while fuel > 0:
    print(f"Fuel left: {fuel}")
    fuel = fuel - 3
print("Empty.")

That prints the four amounts from 10 down to 1, then Empty. A test with > or < is usually safer than != when the variable moves in steps bigger than 1.

You'll write a loop like this for real in the Number Guessing Game project, where Caitlin's game keeps asking until you win or type q. The Handbook has more on infinite loops and on using break to get out of one on purpose.

The project that goes with it

Number Guessing Game is a free project in Act 1, for Caitlin. It runs in your browser with nothing to install, and you don't need an account for it.

More guides