Debugging is like being a detective for your code. When something goes wrong (and it will - even for experienced programmers!), debugging helps you find out why and fix it. The term comes from an old story about finding an actual bug (a moth) in a computer!
In this guide, we'll learn how to use Thonny, a beginner-friendly Python editor that makes debugging much easier.
If you haven't already, download and install Thonny from thonny.org. It's free and works on Windows, Mac, and Linux.
Thonny has a special feature that lets you run your code line by line:
name = input("What's your name? ") age = input("How old are you? ") next_year = age + 1 print(f"Next year, {name} will be {next_year} years old!")
If you run this program, you'll get an error! Let's debug it:
The fix: Convert the input to a number:
name = input("What's your name? ") age = int(input("How old are you? ")) # Convert to integer next_year = age + 1 print(f"Next year, {name} will be {next_year} years old!")
What it means: You've written something Python doesn't understand
Common causes:
Example:
# Wrong: if x > 5 print("Big number") # Right: if x > 5: print("Big number")
What it means: Your code isn't lined up properly
Common causes:
Example:
# Wrong: if x > 5: print("Big number") # Not indented! # Right: if x > 5: print("Big number") # Properly indented
What it means: You're using a variable that doesn't exist
Common causes:
Example:
# Wrong: print(messge) # Typo! # Right: message = "Hello" print(message)
What it means: You're using the wrong type of data
Common causes:
Example:
# Wrong: age = "15" next_age = age + 1 # Can't add number to string! # Right: age = "15" next_age = int(age) + 1
What it means: You're trying to access something that doesn't exist in a list
Common causes:
Example:
# Wrong: fruits = ["apple", "banana", "orange"] print(fruits[3]) # There is no index 3! # Right: fruits = ["apple", "banana", "orange"] print(fruits[2]) # Remember: counting starts at 0
Add print statements to see what's happening in your code:
score = 0 print(f"Score is now: {score}") score = score + 10 print(f"Score after adding 10: {score}")
Python error messages tell you:
Thonny's Assistant panel explains errors in simple language. If you get an error, check what the Assistant says!
If something isn't working, test small pieces of code in the Shell area to understand what each part does.
Sometimes the best debugging tool is taking a short break. Come back with fresh eyes!
Try to find and fix all the errors in this program:
print("Welcome to the number game!) player_name = input("What's your name? ") secret_number = 7 guess = input("Guess a number between 1 and 10: ") if guess = secret_number: print("You got it, player_name!") else: print("Sorry, the number was secret_number")
print("Welcome to the number game!") # Fixed: closed quote player_name = input("What's your name? ") secret_number = 7 guess = int(input("Guess a number between 1 and 10: ")) # Fixed: convert to int if guess == secret_number: # Fixed: use == for comparison print(f"You got it, {player_name}!") # Fixed: use f-string else: print(f"Sorry, the number was {secret_number}") # Fixed: indentation and f-string
Even professional programmers spend a lot of time debugging. Errors are a normal part of learning to code. Each error is a chance to learn something new!
Now that you know how to debug, you can: