👋 Getting Started, lesson 3 of 5
Comments and error messages
Leave notes in your code and learn to read Python's error messages.
8 min
1 exercise
2 quizzes
0/3 solved
Getting Python ready… examples can run in a moment.
A comment starts with #. Python ignores
everything from the # to the end of the line, so
comments are notes for humans: what the code does, or
why.
Putting # in front of a line of code is also a handy
way to switch it off without deleting it (called
commenting out).
Comments are ignored
Remove the # in front of the switched-off line and run it again.
What does this print?
Everyone makes mistakes, so Python tells you exactly what went wrong with an error message (a traceback). Read it from the bottom up:
- The last line names the error type and explains it.
- Just above, you see the line number and the code that failed.
A NameError
Start is printed, then line 2 fails: Python doesn't
know any name called mesage. Text needs quotes!
Change it to print("message") to fix it.
A SyntaxError
A SyntaxError means the code isn't valid Python, so
nothing runs at all. Here a closing ) is missing.
Add it and run again.
Which error does this code cause?
Squash the bugs
This program has two mistakes. Run it, read the error message, fix it, and repeat until it prints:
Python is fun!
Let's keep going