Basic Debugging Guide - Understanding and Fixing Common Errors

Back to Home Page


What is Debugging?

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.


Getting Started with Thonny

If you haven't already, download and install Thonny from thonny.org. It's free and works on Windows, Mac, and Linux.

The Thonny Interface


Using Thonny's Debugger

Step-by-Step Debugging

Thonny has a special feature that lets you run your code line by line:

  1. Write your code in the editor
  2. Click the "Debug current script" button (it looks like a little bug)
  3. Use these buttons to control the debugger:
  4. Watch the Variables panel to see how your data changes

Example: Debugging a Simple Program

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:

  1. Click the debug button
  2. Step through each line
  3. Watch the Variables panel - notice that 'age' is a string, not a number!
  4. The error happens because we can't add 1 to a string

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!")
  

Common Python Errors and How to Fix Them

1. SyntaxError

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")
  

2. IndentationError

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
  

3. NameError

What it means: You're using a variable that doesn't exist

Common causes:

Example:

# Wrong:
print(messge)  # Typo!

# Right:
message = "Hello"
print(message)
  

4. TypeError

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
  

5. IndexError

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
  

Debugging Tips and Tricks

1. Use Print Statements

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}")
  

2. Read Error Messages Carefully

Python error messages tell you:

3. Use Thonny's Assistant

Thonny's Assistant panel explains errors in simple language. If you get an error, check what the Assistant says!

4. Test Small Parts

If something isn't working, test small pieces of code in the Shell area to understand what each part does.

5. Take Breaks

Sometimes the best debugging tool is taking a short break. Come back with fresh eyes!


Practice: Debug This Code

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")
  
Click to see the solution
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
    

Remember: Everyone Makes Mistakes!

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!


What's Next?

Now that you know how to debug, you can:


Back to Home Page