Code Repair and Enhancement
Common Programming Errors
Every Programmer Makes Mistakes
Writing code is a conversation with a computer. Sometimes, you say something it doesn't understand, or you give it an instruction that doesn't quite work as planned. These mistakes, often called "bugs," are a normal and expected part of programming. Learning to find and fix them is a core skill.
Errors generally fall into three categories: syntax errors, runtime errors, and logical errors. Let's break down what each one means.
Syntax Errors
Think of a syntax error as a grammar mistake. Every programming language has strict rules about how to write commands, just like English has rules for constructing sentences. If you break one of these rules, like forgetting a punctuation mark or misspelling a keyword, the computer won't understand what you're trying to do.
The good news is that these are usually the easiest errors to fix. The program won't even run. Instead, it will stop and give you an error message that often points directly to the line of code where the mistake is.
# Let's try to greet a user
user_name = "Alex"
if user_name == "Alex" # Missing a colon at the end
print("Welcome, Alex!")
If you try to run this code, Python will immediately stop and give you a SyntaxError: expected ':'. It's telling you exactly what's missing and where. Simply adding the colon fixes the problem.
Runtime Errors
A runtime error happens while your program is running. The syntax is perfectly correct, but the code asks the computer to do something impossible. The program starts, but then crashes midway through when it hits the impossible instruction.
Common examples include trying to divide a number by zero or attempting to open a file that doesn't exist. Your instructions are grammatically correct, but they don't make sense in the current situation.
# Calculating items per person
items = 100
people = 0
items_per_person = items / people # This line will cause an error
print(f"Each person gets {items_per_person} items.")
This code is syntactically valid. But when it runs, it will crash on the division line and produce a ZeroDivisionError. The error message tells you the type of error and the line number, giving you a clear starting point for your investigation. You can't divide by zero, and the computer lets you know as soon as it's asked to try.
Logical Errors
Logical errors are the trickiest of all. With these, your code has no syntax errors and it runs without crashing. The problem is that it doesn't do what you intended for it to do. It produces the wrong result.
The computer is following your instructions perfectly, but the instructions themselves are flawed. Since the computer has no idea what you were trying to achieve, it can't give you an error message. It just gives you a faulty answer.
# Find the average of three test scores
score1 = 80
score2 = 90
score3 = 100
average = score1 + score2 + score3 / 3 # Incorrect order of operations
print(f"The average score is: {average}")
# Expected output: 90.0
# Actual output: 203.333...
The code runs fine, but the result is clearly wrong. Because of the order of operations in math, the program first divides score3 by 3 and then adds the other two scores. The logic is flawed.
To fix this, you need to tell the computer to do the addition first by using parentheses: (score1 + score2 + score3) / 3. Finding these errors requires you to test your program and think critically about your own code.
The hardest bugs to fix are the ones where your program does exactly what you told it to do.
Basic Bug Hunting
Finding and fixing errors, or "debugging," is a puzzle. Here are a few simple strategies to get started:
-
Read the error message. This is your number one tool for syntax and runtime errors. The message tells you the type of error and often the exact line number where things went wrong.
-
Use print statements. If you have a logical error and aren't sure where the calculation is going wrong, add
print()statements to see the value of your variables at different stages. This helps you trace the flow of data and pinpoint where the logic fails. -
Explain the code to someone else. Often, the act of explaining your code's logic line by line, even to a rubber duck on your desk, helps you spot the mistake yourself. You realize what you intended doesn't match what you wrote.
Ready to test your knowledge on spotting these errors?
A programmer writes code to calculate the average of three numbers: average = num1 + num2 + num3 / 3. The code runs without crashing, but the result is incorrect. What type of error is this?
Which of the following scenarios will most likely cause a SyntaxError?
Becoming good at debugging is about being a detective. It takes practice, but every error you fix makes you a better programmer.