Free online Python course for kids - Programming for Beginners

Fixing Errors

Session 8: Learning from Mistakes

Course Overview | Previous Session | Next Session

Introduction

In our previous session, we created a dog adoption recommendation program using if, elif, and else statements and comparison operators. Today, we’ll focus on an equally important skill: debugging.

Debugging is like solving a mystery where you find and fix errors in your code. It’s a vital skill for any programmer and can often be a learning experience in itself.

Imagine you’re back at the shelter, and Mrs. Thompson is worried the latest software update we made. The program is showing strange results. The recommendations are all mixed up, and the shelter staff are confused.

Mrs. Thompson turns to you and says, “We’ve got some bugs in the system, and I need your help to fix them. Can you figure out what went wrong and correct it? Don’t worry; it’s all part of the programming process!”

You roll up your sleeves and get to work. Debugging isn’t just about fixing errors—it’s about understanding what went wrong, learning from it, and making your code better.

Debugging is crucial because it helps us:

  • Identify and fix errors: Finding mistakes and correcting them is a big part of coding.
  • Understand how our code works: Debugging helps us see how different parts of our code interact.
  • Learn from mistakes: Each error teaches us something new.
  • Develop problem-solving skills: Debugging improves our ability to think critically and solve problems.

Before we dive into debugging, let’s talk about code comments. Comments are like notes you leave for yourself or others to explain what the code does. They don’t affect how the code runs but help you understand it better. In Python, we use the # symbol to start a comment. Anything after # is ignored by Python but visible to us.”

In Python, we use the # symbol to start a comment.
Anything that comes after the # symbol is ignored by Python, but it’s still visible to us.

Here’s an example:

# This is a comment - it explains what the code below does

name = "John" # This comment explains what the variable does
age = 25 # This comment explains what 25 means
print(name + " is " + str(age) + " years old.")

Now it’s time to put your skills to the test. We will walk through three examples where the code contains errors. Your task is to:

  1. Create a New Python File: Open your text editor and create a new file for each example.
  2. Copy the Faulty Code: Paste the provided code into the new file.
  3. Save the File: Save the file with an appropriate name, such as example1.py.
  4. Run the Code: Open your terminal, navigate to the directory where you saved the file, and run the code using python3 example1.py.
  5. Identify and Fix the Errors: Examine the output, identify the errors, and modify the code to fix them.”

name = “John” # This variable stores the user’s name
age = 25 # This variable stores the user’s age
print(nam + ” is ” + age + ” years old.”) # This line prints the user’s name and age

What’s wrong?

  • The variable nam is not defined.
  • The quotes around the strings are incorrect.
  • The age variable needs to be converted to a string.

Fixed Code:

name = "John" # This variable stores the user’s name
age = 25 # This variable stores the user’s age
print(name + " is " + str(age) + " years old.") # Fixed line

Here’s the faulty code:

# This array should store a list of fruits
fruits = ["apple", "banana" "orange", "grape", pear]

# This line should print the list of fruits
print(fruits)

What’s wrong?

  • There is a missing comma between “banana” and “orange”.
  • The string "pear" should be enclosed in quotes.

Fixed Code:

# This array should store a list of fruits
fruits = ["apple", "banana", "orange", "grape", "pear"]

# This line should print the list of fruits
print(fruits)

Here’s the faulty code:

# This variable should store the user's age
age = 25

# This if statement should check if the user is an adult
if age < 18:

  # This line should print a message if the user is an adult
  print("You're an adult!")
else:
  # This line should print a message if the user is not an adult
  print("You're not an adult yet.")

What’s wrong?

  • The condition in the if statement is incorrect for checking adulthood (it should check age >= 18).

Fixed Code:

# This variable should store the user's age
age = 25

# This if statement should check if the user is an adult
if age >= 18:
  # This line should print a message if the user is an adult
  print("You're an adult!")
else:
  # This line should print a message if the user is not an adult
  print("You're not an adult yet.")

Feeling overwhelmed or confused while debugging is normal. Remember, each mistake is a learning opportunity. As you continue to practice, you’ll become more skilled at spotting and fixing errors. Keep going, and don’t be discouraged—every error you fix makes you a better programmer.


Course Overview | Previous Session | Next Session