Errors are normal. Learn to read a traceback from the bottom up and fix SyntaxError, NameError, TypeError and ValueError.
About 30 minBeginner3 exercisesFree
You will be able to
Read a traceback to find the line that failed, the error type and the message
Tell a SyntaxError (the code can't be understood) from an error that happens while the program runs
Fix common causes of NameError, TypeError and ValueError
Treat error messages as clues rather than failures
Every programmer sees error messages every day, including people who have written Python for twenty years. An error doesn't mean you're bad at this. It means Python found something it couldn't do, and it's telling you where and why. Learning to read that message is one of the most valuable skills in this whole course: it turns "it doesn't work" into "line 4 adds text to a number".
Anatomy of a traceback
When a program stops with an error, Python prints a report called a traceback. Here is the one you get from running print("Total: " + 5) on line 2 of a program:
Traceback (most recent call last):
File "main.py", line 2, in <module>
print("Total: " + 5)
~~~~~~~~~~^~~
TypeError: can only concatenate str (not "int") to str
Read it from the bottom up:
The last line names the error type (TypeError) and gives a message describing the problem. Start here.
Above it, Python shows the line of code that failed, often with markers such as ^ or ~ under the exact part.
File "main.py", line 2 tells you where that line is. in <module> means "in the main part of the program".
The first line, Traceback (most recent call last):, is a heading. It's the same every time.
Python Quest shows the same information in the output panel and highlights the line in the editor.
Two moments when errors happen
Before anything runs. Python first reads your whole file to check its grammar, the rules of the language, called syntax. If something breaks those rules, such as a missing bracket or quote, you get a SyntaxError (or its relative IndentationError) and no line runs at all, not even the first.
While the program runs. If the grammar is fine, Python starts running lines. An error on line 5 means lines 1 to 4 already ran, and their output is visible above the traceback.
Try it: Run it, find the line the error points to, and add the missing closing parenthesis.
The message says '(' was never closed. A SyntaxError sometimes points at the line after the real mistake, because Python only realises something is missing when the next line doesn't fit. If the reported line looks fine, check the one above it.
The errors you'll meet most often
Error
What it means
Typical causes
SyntaxError
Python can't understand the code
missing ) or quote, missing comma, = where you meant something else
IndentationError
spaces at the start of a line are wrong
a line indented for no reason
NameError
a name Python doesn't know
typo (prnt, totl), text without quotes, a variable used before it's assigned
TypeError
an operation on the wrong kind of value
"Total: " + 5, subtracting strings, len(42)
ValueError
the kind of value is right but the value itself isn't
int("2.5"), int("ten")
ZeroDivisionError
dividing by zero
total / 0
A NameError: text without quotes
city ="Jeddah"print("Branch:", city)print("Region:", Western)
Try it: Read the message, then fix line 3 so it prints Region: Western.
Western has no quotes, so Python treats it as a variable name, and no variable has that name. Python 3.14 often adds a suggestion such as Did you mean: ...? when the name is close to one it knows, which is a big help with typos.
Try it: Change the input so the program works. Which inputs does int() accept?
A calm routine for any error
Read the last line. What type of error, and what does the message say?
Go to the line number. Look at the exact part marked in the traceback.
Ask what Python saw. What value or name did it have at that moment? Is it text or a number? Is the name spelled the same as where it was assigned?
Fix one thing, then run again. A new error after a fix usually means progress: Python got further.
Guided practice · ●difficulty 1 of 5 · about 3 min
Fix a SyntaxError
This invoice program reports:
SyntaxError: '(' was never closed
Find the line it points to and fix it, so the program prints:
Invoice 2207
Client: Oasis Dental
Amount: 1450 SAR
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Challenge · ●●difficulty 2 of 5 · about 8 min
Fix three errors in a delivery quote
A courier's delivery quote program has three separate errors, each a different type. When it works, it asks for the parcel weight in kg (a whole number) and prints the cost at 7 SAR per kg plus a fixed 15 SAR fee. For input 4:
Service: Express
Delivery cost: 43 SAR
Run it, read each error message, fix that problem, and run again until it works.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Edge cases · ●●difficulty 2 of 5 · about 5 min
An error that only some inputs cause
A fuel station program asks for the litres pumped and prints the price at 2.33 SAR per litre. It works when the customer pumps a whole number of litres, but a customer who pumped 12.5 litres got this:
ValueError: invalid literal for int() with base 10: '12.5'
Fix the program so it works for both whole and decimal amounts. For input 20 it prints:
To pay: 46.60 SAR
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Summary
A traceback ends with the error type and message; directly above is the failing line and its line number.
A SyntaxError is found before anything runs; other errors happen when the faulty line is reached.
NameError: unknown name (typo, missing quotes, used before assigned). TypeError: wrong kind of value. ValueError: right kind, unusable value.