CMU 15-112: Fundamentals of Programming and Computer Science
Class Notes: Debugging
In class today, we'll have a short discussion on some helpful debugging techniques, especially using print statements strategically.
The Problem: We Have a Bug!
Here is an example with a bug intentionally added:
# THIS CODE HAS A BUG (ON PURPOSE)!!!!
# When you run it, it will hang (run forever)!!!!
def isPrime(n):
if (n < 2):
return False
if (n == 2):
return True
if (n % 2 == 0):
return False
maxFactor = round(n**0.5)
for factor in range(3,maxFactor+1,2):
if (n % factor == 0):
return False
return True
def nthPrime(n):
found = 0
guess = 0
while (found <= n):
guess += 1
if (isPrime(guess)):
found + 1
return guess
print('The next line will hang (run forever):')
print(nthPrime(5))
A Helpful Approach: Print Statement Debugging
Now, let's add a well-placed print statement to shine a light on what the bug is:
# THIS CODE STILL HAS A BUG (ON PURPOSE)!!!!
# When you run it, it will hang (run forever)!!!!
def isPrime(n):
if (n < 2):
return False
if (n == 2):
return True
if (n % 2 == 0):
return False
maxFactor = round(n**0.5)
for factor in range(3,maxFactor+1,2):
if (n % factor == 0):
return False
return True
def nthPrime(n):
found = 0
guess = 0
while (found <= n):
print(guess, found) ### <--- THIS is our well-placed print statement!
guess += 1
if (isPrime(guess)):
found + 1
return guess
print('The next line will hang (run forever):')
print(nthPrime(5))
Even Better: Print Statement Debugging with locals() + input()
Check out this perhaps even-more-helpful approach:
# THIS CODE STILL HAS A BUG (ON PURPOSE)!!!!
# When you run it, it will hang (run forever)!!!!
def isPrime(n):
if (n < 2):
return False
if (n == 2):
return True
if (n % 2 == 0):
return False
maxFactor = round(n**0.5)
for factor in range(3,maxFactor+1,2):
if (n % factor == 0):
return False
return True
def nthPrime(n):
found = 0
guess = 0
while (found <= n):
print(locals()) ### <--- THIS is our well-placed print statement!
input() ### <--- THIS pauses until we hit Enter. Sweet!
guess += 1
if (isPrime(guess)):
found + 1
return guess
print('The next line will hang (run forever):')
print(nthPrime(5))
Now that was super helpful!!!!