Describe Problem
def my_function():
for i in range(1, 20):
#it actually goes from 1 all the way up to 19 but not including 20
if i == 20:
print("You got it")
my_function()
# Solution
def my_function():
for i in range(1, 21):
if i == 20:
print("You got it")
my_function()
Reproduce the Bug
from random import randint
dice_imgs = ["❶", "❷", "❸", "❹", "❺", "❻"]
dice_num = randint(1, 6)
# randint(a, b) : Return random integer in range [a, b], including both end points.
print(dice_imgs[dice_num])
# Solution
from random import randint
dice_imgs = ["❶", "❷", "❸", "❹", "❺", "❻"]
dice_num = randint(0, 5)
print(dice_imgs[dice_num])
Play Computer
year = int(input("What's your year of birth?"))
if year > 1980 and year < 1994:
print("You are a millenial.")
elif year > 1994:
#When input'1994', print noting
print("You are a Gen Z.")
# Solution
year = int(input("What's your year of birth?"))
if year > 1980 and year < 1994:
print("You are a millenial.")
elif year >= 1994:
print("You are a Gen Z.")
Fix the Errors
age = input("How old are you?")
# we're getting the input from the console as a string, so we must that input string to an integer object in order to do numerical operations.
if age > 18:
print("You can drive at age {age}.")
#indent error & f-string
# Solution
age = int(input("How old are you?"))
if age > 18:
print(f"You can drive at age {age}.")
Print is Your Friend
pages = 0
word_per_page = 0
pages = int(input("Number of pages: "))
word_per_page == int(input("Number of words per page: "))
# two equal sigh gets evaluated and becomes either true or false.
total_words = pages * word_per_page
print(total_words)
# Solution
pages = 0
word_per_page = 0
pages = int(input("Number of pages: "))
word_per_page = int(input("Number of words per page: "))
total_words = pages * word_per_page
print(total_words)
Use a Debugger
def mutate(a_list):
b_list = []
for item in a_list:
new_item = item * 2
b_list.append(new_item)
# to fix this code is just to indent that line over.
print(b_list)
mutate([1,2,3,5,8,13])
#output
[26]
# Solution
def mutate(a_list):
b_list = []
for item in a_list:
new_item = item * 2
b_list.append(new_item)
print(b_list)
mutate([1,2,3,5,8,13])
#output
[2, 4, 6, 10, 16, 26]
Final debugging tips
exercise1
number = int(input("Which number do you want to check?"))
if number % 2 = 0:
print("This is an even number.")
else:
print("This is an odd number.")
# Solution
number = int(input("Which number do you want to check?"))
if number % 2 == 0:
print("This is an even number.")
else:
print("This is an odd number.")
exercise2
year = input("Which year do you want to check?")
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print("Leap year.")
else:
print("Not leap year.")
else:
print("Leap year.")
else:
print("Not leap year.")
# Solution
year = int(input("Which year do you want to check?"))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print("Leap year.")
else:
print("Not leap year.")
else:
print("Leap year.")
else:
print("Not leap year.")
exercise3
for number in range(1, 101):
if number % 3 == 0 or number % 5 == 0:
print("FizzBuzz")
if number % 3 == 0:
print("Fizz")
if number % 5 == 0:
print("Buzz")
else:
print([number])
# Solution
for number in range(1, 101):
if number % 3 == 0 and number % 5 == 0:
print("FizzBuzz")
elif number % 3 == 0:
print("Fizz")
elif number % 5 == 0:
print("Buzz")
else:
print(number)