Unit 3 · Decisions, Decisions · Lesson 30 of 57
Traceback Trouble
Traceback trouble
By now you've seen Python yell at you a few times. Those big red messages are called tracebacks, and I know they are very scawwy, but they're actually Python trying to help you!
Here's one:
Traceback (most recent call last):
File "main.py", line 3, in <module>
print("Hello " + nme)
NameError: name 'nme' is not defined
Read it from the bottom up:
- The last line is the most important. It tells you what went wrong: a
NameError, because there's no variable callednme. - The line above it shows you the exact code that broke.
line 3tells you where to look.
So: line 3, nme doesn't exist... Oops! It should be name.
The usual suspects
Every programmer sees these constantly. Yes, even the ones who've been doing it for years. It's more about just knowing what you did wrong and quickly fixing it!
"You miss 100% of the lines you don't code." - Karl
Amer wrote a program. It has three bugs. (We're not surprised either.)
Run it, read the traceback, fix the bug it points to, and run it again. Keep going until it works!
When you type 2009, it should print:
You are 17
That's old enough!
Python exercise
Write the program in an editor next to the lesson, run it, and have it checked automatically. Create a free account to do this part.
Do this lesson for real
With a free account you get the terminals, challenges and checks that go with the lessons in Intro to Python, and your progress is saved as you go. CyberAcad is a 501(c)(3) nonprofit, so there is nothing to pay.