The 3 Types of Errors
As we move from single lines to longer programs, you will encounter bugs. Identifying which type of bug you have is 90% of the battle.
1. Syntax Error
Grammar mistakes. Python doesn't know how to read your code.
Ex: Missing parenthesis, typos in keywords.
2. Runtime Error
The code reads fine, but crashes while running.
Ex: Dividing by zero, using a variable that isn't defined.
3. Logic Error
The code runs, but gives the wrong answer.
Ex: Using + instead of *, order of operations.
Fixing Syntax
This code attempts to print a message, but it won't run. Can you spot the syntax error?
Need a Hint?
Solution:
You missed the closing parenthesis on line 3.
print(message)
The Quote Jam (Intermediate)
Strings in Python must be surrounded by matching quotes. But what if the text itself contains a quote?
Need a Hint?
Solution:
Use double quotes on the outside so the single quote inside is treated as text.
print("It's a beautiful day")
The Naming Conflict (Advanced)
Variables usually hold data, but they must follow strict naming rules. This code fails because the variable name is illegal.
Need a Hint?
Solution:
Variables can't start with numbers.
prize_1st = 1000
Fixing Runtime Errors
This code is grammatically correct, but it fails during execution. Scenario: You have 50 gold coins to share among a group of people.
Need a Hint?
people variable. Can you divide by that number?
Solution:
You cannot divide by zero. Update people to a real number!
people = 5
The Ghost Variable (Intermediate)
Python is case-sensitive. Value and value are
different things. This code crashes because it can't find the variable.
Need a Hint?
my_score in the print statement.
Solution:
Match the casing exactly.
print(my_score)
The Type Clash (Advanced)
You can add numbers to numbers, and strings to strings. But mixing them causes a TypeError.
Need a Hint?
Solution:
Make the number a string if you want to glue them together.
cost = "5" + " Dollars"
Fixing Logic Errors (The Dangerous Ones)
Logic errors are dangerous because the program doesn't crash. It just lies to you.
Goal: Calculate the average of 10 and 20. The answer should be 15.
Need a Hint?
Solution:
You need parentheses to force the addition to happen first.
average = (num1 + num2) / 2
The Lost Power-Up (Intermediate)
You picked up a health potion, but your health didn't go up! Python calculates the answer, but if you don't save it, it's gone forever.
Need a Hint?
health + potion does the math, but doesn't update the variable. You need
to use the = sign.
Solution:
Assign the result back to the variable.
health = health + potion
The Bad Discount (Advanced)
A store wants to give a 20% discount on a $200 item. Use logic to figure out why this calculation is wrong.
Need a Hint?
Solution:
Calculate the discount amount first.
sale_price = price - (price * discount_percent)
Capstone: The Travel Planner
Good programmers don't write 50 lines at once. They write 5, test, then write 5 more. Let's compare a "lazy" approach vs. a "structured" approach.
The Problem:
You are planning a 5-night trip to Paris. You have saved $1,000 USD.
You need to write a program to:
- Convert your dollars to Euros (rate: 0.85).
- Calculate the total cost of the hotel (100 Euros/night).
- Calculate the total cost of food (50 Euros/day).
- Subtract the expenses from your budget to see what's left.
The "Lazy" One-Liner
This programmer hates typing. They use single-letter variables (`b`, `n`, `r`) and try to do all the
math in one line.
The Bug: The calculation result is wrong. It implies they have more money after
the trip than expected!
Need a Hint?
The Structured Approach
This version breaks the problem into steps. We've started the code for you, but left the
calculations blank.
Your Task: Replace the ?? with the correct math variables.