My First Python Errors
For my debugging task, I deliberately wrote a line of Python with a mistake: print("Python is fun) - I left out the closing quote. When I ran python3 debug.py, I got my first error: SyntaxError: unterminated string literal (detected at line 2).
What confused me:
- Line 1 was correct, but it did not print. I expected Python to run it before reaching the broken line. I learned that Python reads the whole file first, and if one line has a grammar mistake, nothing runs at all.
- The error message looked scary. Words like "unterminated string literal" sounded like another language, and I did not know what the ^ arrow was pointing at.
- Line 3 also had a mistake (a NameError), but it did not show up first. I learned that Python reports one error at a time, from the top down.
- The missing quote was hard to spot, because it was such a tiny character.
How I found the solution: I read the line number (line 2), looked at where the arrow pointed, and noticed that I had opened a quote but never closed it. I put the closing quote back and ran the program again. Then the NameError appeared on line 3, because I wrote hello without quotes, so Python thought it was a name it should already know. I fixed it by adding quotes, and all three lines printed.
What I learned: when I see an error in python, I should read the line number and the last line of the message first. An error is not a sign that I failed. It is Python telling me exactly where to look.
#python #error #learned
