3.8. Common While Loop Errors#
3.8.1. Infinite While Loop#
One of the easiest mistakes to make with a while loop is having a condition
that is always True. This results in an infinite while loop!
For example:
i = 0
while i < 3:
print(i)
What’s happening here?
We can see that the condition we have set for our while loop is i < 3.
At the first iteration i = 0. This means the condition is True. We
execute the code block:
print(i)
This means that we output:
0
At the second iteration i = 0. This is because we did not update i.
This means the condition is True. Again, we execute the code block and we
output:
0
At the third iteration i = 0, at the fourth iteration i = 0,
and so on. This is because we never updated i. This results in the loop
never ending!
3.8.2. Not initialising your iteration variable#
If you forget to initialise your iteration variable you’ll get a NameError (A variable of the specified name cannot be found).
See what happens when you run the following code.
while i < 3:
print(i)
i = i + 1
The code above produces the following error message.
Traceback (most recent call last):
File "/home/main.py", line 1, in <module>
while i < 3:
^
NameError: name 'i' is not defined. Did you mean: 'id'?
The error message tells us the error is on
line 1and comes from thewhile i < 3:.It also tells us that we have a NameError, which means a variable hasn’t been defined. In this case it’s the variable
i. The program is trying to check whetheriis less than 3, butidoes not exist.
3.8.3. Incorrect indentation#
It’s important that you get the indentation correct. Anything that is inside the loop, i.e. the code that is repeated, should be indented.
If nothing is indented you will get an IndentationError (The indentation is not syntactically valid).
i = 0
while i < 3:
print(i)
i = i + 1
File "/home/main.py", line 3
print(i)
^
IndentationError: expected an indented block after 'while' statement on line 2
You can also get an IndentationError if your indentation isn’t consistent.
In the example below line 3 has 5 spaces in front of it while line 4
only has 4 spaces in front of it. The number of spaces in front of these lines
need to be the same.
i = 0
while i < 3:
print(i)
i = i + 1
File "/home/main.py", line 4
i = i + 1
^
IndentationError: unindent does not match any outer indentation level
Question 1
Which of the following code snippets will result in the output
0
1
2
i = 0 while i < 3: print(i)
i = 0 while i < 3: print(i) i = i + 1
i = 0 while i < 3: print(i) i = i + 1
while i < 3: print(i) i = i + 1
Solution
i = 0
while i < 3:
print(i)
Incorrect. Will result in an infinite loop.
i = 0
while i < 3:
print(i)
i = i + 1
Correct.
i = 0
while i < 3:
print(i)
i = i + 1
Incorrect. This results in an IndentationError because the indentation of line 3 doesn’t match the indentation of line 4.
while i < 3:
print(i)
i = i + 1
Incorrect. This results in an IndentationError because the line directly below where the while loop is defined (line 2) is not indented.
Question 2
Will the following two programs produce the same output?
Program 1
i = 0
while i < 3:
print(i)
i = i + 1
print(i)
Program 2
i = 0
while i < 3:
print(i)
i = i + 1
print(i)
Solution
Solution is locked
Question 3
What is wrong with the following code?
i = 0
while i < 3:
print(i)
print(2*i)
i = i + 1
The indentation on
line 4does not match the indentation online 3andline 5.The iteration variable
ihas not been initialised.The indentation on
line 5is incorrect. It should not be indented.This code will result in an infinite loop.
Solution
Solution is locked
Question 4
Which of the following result in an infinite loop? Select all that apply.
i = 0 while i < 3: print(i) i = 0
i = 0 while i < 3: print(i)
i = 0 while i < 3: print(i) i = i + 1
i = 0 while i < 3: i = i + 1 print(i)
Solution
Solution is locked
Code challenge: Forever i
You have been provided with some code. However, the code has some bugs!
i = 0
while i < 5:
print('i')
Debug this code!
The output of the program should look like this:
0
1
2
3
4
Solution
Solution is locked
Code challenge: Count Up
Write a program that reads in an integer, and counts from 0 up to that integer.
Example 1
Enter a number: 3
0
1
2
3
Example 2
Enter a number: 5
0
1
2
3
4
5
Solution
Solution is locked
Code challenge: Baby Shark
Write a program that asks the user for the number of baby sharks and then prints
Baby shark, doo, doo, doo, doo, doo, doo
the number of times specified. The program should finish by printing
Baby shark!
Here are some examples of how your program should run.
Example 1
How many baby sharks? 3
Baby shark, doo, doo, doo, doo, doo, doo
Baby shark, doo, doo, doo, doo, doo, doo
Baby shark, doo, doo, doo, doo, doo, doo
Baby shark!
Example 2
How many baby sharks? 8
Baby shark, doo, doo, doo, doo, doo, doo
Baby shark, doo, doo, doo, doo, doo, doo
Baby shark, doo, doo, doo, doo, doo, doo
Baby shark, doo, doo, doo, doo, doo, doo
Baby shark, doo, doo, doo, doo, doo, doo
Baby shark, doo, doo, doo, doo, doo, doo
Baby shark, doo, doo, doo, doo, doo, doo
Baby shark, doo, doo, doo, doo, doo, doo
Baby shark!
Solution
Solution is locked
Code challenge: Double Until 10000
Write a program that starts with 1 and continues to double it until it hits a number greater than 10000. Your program should print each value that is under 10000.
The first 5 lines of your output should look like this:
1
2
4
8
16
Solution
Solution is locked
Code challenge: Double Until…
Write a program that asks the user for a number \(n\). Your program should then start with 1 and continue doubling it until it reaches \(n\) or greater. Your program should then print out the number of times it had to double the starting number to reach \(n\).
Example 1: \(1 \rightarrow 2 \rightarrow 4\)
n: 4
You have to double 2 times to reach 4
Starting with 1 you double it once and you get 2, then you double it again to get 4. So you have to double it 2 times to get 4. (Count the number of arrows in \(1 \rightarrow 2 \rightarrow 4\).)
Example 2: \(1 \rightarrow 2 \rightarrow 4 \rightarrow 8 \rightarrow 16\)
n: 16
You have to double 4 times to reach 16
There are 4 arrows in \(1 \rightarrow 2 \rightarrow 4 \rightarrow 8 \rightarrow 16\), so you have to double 4 times to reach 16.
Example 1: \(1 \rightarrow 2 \rightarrow 4 \rightarrow 8 \rightarrow 16 \rightarrow 32 \rightarrow 64\)
n: 50
You have to double 6 times to reach 50
This time there are 6 arrows, so you have to double 6 times to reach 50 or more.
Hint
While you’re writing your program you might want to keep track of your doubled number by printing it out as you go. E.g. for example 1 your output might look like this:
n: 4
1
2
4
You have to double 2 times to reach 4
Once you’re happy that your code works you can delete this print statement.
Solution
Solution is locked