Lesson 10: Common Beginner Mistakes
Save time and frustration by learning how to identify and fix the most common Python errors made by beginners.
Lesson 10: Common Beginner Mistakes
Making mistakes is a mandatory part of learning to code. Even professionals with 20 years of experience get errors every single day. The difference is that professionals know how to read the error messages. In this lesson, we will look at the "Most Wanted" list of beginner mistakes and how to solve them.
Lesson Overview
In this lesson, we will cover:
- Syntax Errors: Problems with your "grammar."
- Runtime Errors: Problems while the code is running.
- Logical Errors: When the code runs, but gives the wrong answer.
- Indentation Issues: The #1 cause of Python headaches.
- Naming Blunders: Using variable names that don't exist.
1. The Syntax Error (The Typos)
This happens when you write something that isn't valid Python.
Common Culprits:
- Missing a colon
:at the end of anif,for, orwhilestatement. - Missing a closing parenthesis
)or quote". - Using
=instead of==for comparisons.
# MISTAKE
if age > 18
print("Adult")
# FIX
if age > 18:
print("Adult")
2. Indentation Errors (The Spaces)
In Python, spaces matter. If your code isn't aligned correctly, Python doesn't know where a block starts or ends.
# MISTAKE
if True:
print("This will crash")
# FIX
if True:
print("This works!")
Pro Tip: Always use the Tab key or exactly 4 spaces. Don't mix them!
3. NameError (The "Who?")
This happens when you try to use a variable or function that hasn't been defined yet.
# MISTAKE
print(my_score) # Error if my_score wasn't created yet!
# FIX
my_score = 100
print(my_score)
Common cause: A small typo. If you name it score but type scroe, you’ll get a NameError.
4. TypeError (The "Mismatched Types")
Trying to do something with a data type that doesn't support it (e.g., adding a string to an integer).
# MISTAKE
age = input("Enter age: ") # Returns a string "20"
print(age + 5) # Error: Can't add string to integer!
# FIX
age = int(input("Enter age: "))
print(age + 5)
5. Logical Errors (The Silent Killers)
The most dangerous error because Python won't tell you anything is wrong. Your code runs perfectly, but the math is wrong.
# MISTAKE (Goal: Average of two numbers)
average = 10 + 20 / 2 # Result is 20, because 20/2 happens first!
# FIX (Use Parentheses)
average = (10 + 20) / 2 # Result is 15.0
How to Debug Like a Pro
- Read the Bottom Line: The actual error name is always at the very bottom of the error message.
- Lookup the Line Number: Python tells you exactly which line failed.
- Print Everything: When in doubt, use
print()to see what your variables actually equal at that moment.
Practice Exercise: The Bug Hunt
Copy this broken code into a file named fix_me.py and fix all the errors until it runs:
# BROKEN CODE
name = input("Enter your name: ")
age = input("Enter your age: ")
if name == "Admin"
print("Welcome Admin")
if age > 18:
print("You are allowed to enter)
Quick Knowledge Check
- What is a
SyntaxError? - How many spaces should you usually use for an indentation?
- What error do you get if you mistype a variable name?
- Why are logical errors harder to find than syntax errors?
Key Takeaways
- Error messages are tutorials, not insults!
- Always check your colons, parentheses, and quotes.
- Indentation is mandatory for structure.
- Logical errors require step-by-step checking of your math.
What’s Next?
Now that you can handle the bumps in the road, it's time to build something real. In Lesson 11, we’ll build several Hands-on Projects to put all your Module 1 skills to the test!