Hoppy is flying the airship when suddenly... alarm bells ring! The control panel is flashing red.
"Alert! Code integrity compromised!"
To save the ship, we must understand why it crashed. Errors are not failures; they are clues.
Types of Crashes
Just like a machine can break in different ways, code has different error types:
- SyntaxError: You broke the rules of the language (like bad grammar).
- NameError: You tried to use something that doesn't exist (like calling a ghost).
- TypeError: You tried to combine things that don't match (like adding text to a number).
# SyntaxError (Missing colon) if True pass # NameError (Typo) print(x) # x is not defined # TypeError (Math with text) "score: " + 100
1
Fix the SyntaxError
The greet function definition is missing a colon :. Add it.
2
Fix the NameError
The variable message is defined, but we tried to print mesage. Fix the typo.
3
Fix the TypeError
We cannot add the string "Power level: " to the integer 9000. Convert the number to a string using str(9000).
Read the "Crystal Ball" (Terminal) output carefully. It points to the exact line number where the crash happened!
Suggested SolutionExpandCollapse
ExpandCollapse
Solution:
def greet(name):
print("Welcome, " + name) # SytaxError fixed: added :
message = "System Online" # NameError fixed: typing mesage -> message
print(message)
# TypeError fixed: turn 9000 into "9000"
print("Power level: " + str(9000))Advanced TipsWant more? Click to expandClick to collapse
F. Debugging Wisdom
- Read the Error Name: It usually tells you exactly what went wrong.
- Check the Line Number: The error message tells you where to look (
line 5). - Don't Panic: Even professional architects break code every day.
Loading...
Terminal
Terminal
Ready to run...