Debugging while
Loops
Loops are so important and debugging them can be so tricky that we are going to cover a few common mistakes and debugging strategies specific to loops.
As always, our general strategies for debugging python code still apply here.
Scenario 1: did not initialize our tracker variable
# TODO: run this code!
while x < 5:
print(x)
x += 1
Running the above code will give you an error message similar to:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-e02499b1260a> in <module>()
1 # TODO: run this code!
2
----> 3 while x < 5:
4 print(x)
5 x += 1
NameError: name 'x' is not defined
This message tells us that at line 3, x
is not defined.
The fix: add a line, such as x = 0
before the start of the loop.
x = 0
while x < 5:
print(x)
x += 1
Scenario 2: while loop is not executing
# TODO: run this code!
x = 0
while x > 5:
print(x)
x += 1
Running this code will not give you an error, but it will also give you no output. The fact that there is no output indicates that your loop did not run, not even once. Notice that this code does finish executing, the loop just didn’t run. This is a bit clearer if we add a print
statement after the loop (a great step to take when debugging!).
# TODO: run this code!
x = 0
while x > 5:
print(x)
x += 1
# start debugging this code by printing the value of x after the loop
print(x)
The fix: this is most often due to either the initial value of the tracker variable or the logical expression being incorrect. What is happening here is that the very first time x > 5
gets tested, it evaluates to False
so the loop does not run at all.
In this case, we would want to change the logical expression to x < 5
.
A question for you: what would happen if instead of changing the logical expression, we changed the initial value of x
to a number 5 or greater as in the code snippet below?
x = 5 # start at 5 instead of 0
while x > 5:
print(x)
x += 1
# start debugging this code by printing the value of x after the loop
print(x)
Scenario 3: infinite loop
This is the most common and, arguably, the most frustrating kind of error that occurs with while
loops. Take a look at the following code:
x = 0
while x < 5:
print(x)
x -= 1
You can tell that you have an infinite loop if the kernel “hangs” (in jupyter notebooks, this is indicated by the *
icon in the brackets to the left of the cell, as in In [*]:
) or if it produces infinite output. These types of loops require you to manually stop the kernel.
The fix: these errors are because either the update step of your while loop is not happening as you wish (you may have forgotten it entirely) or the logical expression testing are we done? isn’t testing what you think that it’s testing.
Start debugging these scenarios by finding the code that updates the variable that you are testing in your logical expression:
x = 0
while x < 5:
print(x)
x -= 1 # This is the update since it changes the value of x
Now, our next questions are:
- is this update happening when we think that it should happen?
- will the logical expression ever evaluate to False?
In this code, our update is happening on every iteration but x < 5
will never be False because the value of x
only gets smaller. As in the scenario above, how you fix this depends on what you want your code to do:
x = 0
while x > -5: # fix by changing the logical expression
print(x)
x -= 1
or:
x = 0
while x < 5:
print(x)
x += 1 # fix by changing the update