Skip to main content

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:

  1. The last line is the most important. It tells you what went wrong: a NameError, because there's no variable called nme.
  2. The line above it shows you the exact code that broke.
  3. line 3 tells you where to look.

So: line 3, nme doesn't exist... Oops! It should be name.

The usual suspects

ErrorWhat it usually means
SyntaxErrorPython can't even read it. A missing :, quote or parenthesis.
IndentationErrorSomething isn't pushed in (or is pushed in too far).
NameErrorYou used a variable or function that doesn't exist. Usually a typo!
TypeErrorYou mixed types that don't mix, like "5" + 5.
ValueErrorThe type is right, but the value isn't, like int("hello").

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.