Home › Learn › Python › First steps

Reading error messages

Lesson 5 of 40. Learn to read a traceback by fixing three broken lines.

When something goes wrong, Python stops and prints a traceback. The last line is the important one: the kind of error and a message. NameError: name 'totl' is not defined means you used a name that was never assigned, usually a typo. TypeError: can only concatenate str (not "int") to str means you tried to glue text and a number with +; convert the number with str() or use an f-string. SyntaxError means Python could not even read the line, often a missing quote or parenthesis; the caret points near the problem.

The line number in the traceback is where to look. Fix one error, run again, and the next one appears; that is normal.

Your task

The starter has three bugs. Fix them so the program prints Total: 42 on the first line and Done on the second, without changing what it is trying to do.

Hint 1

Run it and read the last line of the error. Fix that one thing, then run again.

Hint 2

A number cannot be added to text with +. Use print(f"Total: {total}") or str(total).

Show the solution
price = 40
tip = 2
total = price + tip
print(f"Total: {total}")
print("Done")

    Runs CPython compiled to WebAssembly in this tab (12 MB download the first time, then cached); nothing is sent anywhere.