Project 3 of 13, for Neil
Reading Tracebacks
Five broken racing and rocket scripts from Neil's first term, and the four errors every Python programmer meets.
No account needed. Your work is kept in this browser until you sign up.
The brief
- NeilThese are five scripts from my first term, karting, rockets and one about the clock, and every one of them crashes.
- NeilI kept them on purpose, honestly they're the best teaching material I've got.
- PaulWhen Python breaks it leaves you a note, and today you're going to learn how to read it.
Neil's first-term scripts: a race-day board for his karting club, a rocket launch date, a glider's wings, and the clock. Five small scripts, and each one crashes. Read the traceback, find the line, fix it. Between them they hold the four errors you will meet most: SyntaxError, NameError, TypeError and ValueError. Each one is explained just before you fix it. When the last script runs, the race-day board prints again.
Race day at the indoor track Practice 09:30 Race 16:00
What you learn
The lesson, step by step
When a program crashes
When a line can't be done, Python stops the program there and prints a traceback: its note about what went wrong. Nothing is broken for good, and it's the most useful thing on the screen.
Run the program on the right. Line 1 prints. Line 2 tries to divide by zero, which can't be done, so the program stops. Line 3 never runs.
Read a traceback from the bottom:
- The last line names the error and says what went wrong:
ZeroDivisionError: division by zero. - Just above it are the line number,
line 2, and that line copied out.
The usual mistake is starting at the top. The first line,
Traceback (most recent call last), is the same every time.- The last line names the error and says what went wrong:
Where did it break?
Everything here is from the last two projects. Go through it a line at a time, as Python does.
Reading from the bottom
- PaulStart reading from the bottom, because the last line's where it actually hurt.
This is what the Quick check's program printed. The labels beside it are the order Paul reads a traceback in. Use the same order on each of Neil's five scripts.
The last line is the hardest to read. Invalid literal for int() means "text
int()can't read", and base 10 means ordinary digits. So:int()was given'4.5', and that isn't a whole number.When Python can't read the code
A SyntaxError means Python couldn't read the code. Syntax is the grammar of a language: where the brackets, quotes and commas go.
Python reads the whole file before it runs any of it. So with a SyntaxError nothing runs at all, not even the lines above the mistake. Run the program on the right: line 1 is fine, but it doesn't print.
The note is shorter than a traceback. It names the line, copies it out, and puts
^marks under the part Python couldn't read. Here,Perhaps you forgot a comma?is right: it should beprint("Laps:", 12).The usual trap: the mistake can be just before the line named. A bracket left open on one line often shows up on the next.
Does line 1 run?
Line 1 is fine. The string on line 2 has no quote at its end. Remember when Python reads the file.
Script one, the header
- NeilFirst up is the header for my race-day board, and it won't even start.
- NeilSee the duck on my desk? When you're stuck, try explaining the problem to it out loud.
- NeilIt won't answer, obviously, but you'll usually hear yourself spot it halfway through.
Neil's header for his karting club's race-day board. Run it and read the last line:
'(' was never closedmeans a bracket was opened and never shut. The^points at it.Fix it so the header prints:
KARTING CLUB, RACE DAY Helmets on, visors downA name Python doesn't know
A NameError means Python met a name it hasn't been told about. There are three usual reasons:
- a typo, like
trakfortrack - a variable used before the line that gives it a value
- text with its quotes missing, so Python reads the word as a name
Names must match exactly, capital letters included:
Raceandraceare two different names.Run the program on the right. The last line of the traceback says
NameError: name 'trak' is not defined. Did you mean: 'track'?Python often suggests the name you meant, right at the end, so read the line to the end.- a typo, like
Script two, the times
- NeilThis was my first week here. I time laps to the hundredth of a second and I couldn't even spell time.
Neil's script puts the Clocktower clock, stuck at 3:14, next to the real time. Run it and read the last line of the traceback, all the way to the end.
Fix it so it prints:
Clock says: 03:14 Actually: 09:30The wrong type for the job
A TypeError means a value is the wrong type for what you asked. The classic is
+with a string on one side and a number on the other.+joins two strings, and it adds two numbers. Given one of each, Python won't guess which you meant, so it stops. In the program on the right,lapsholds the int 12, and line 2 tries to join it to a string.The last line says
can only concatenate str (not "int") to str. Concatenate means join: Python can only join a string to another string, and it was given an int.The usual mistake is expecting Python to turn the number into text by itself. It never does, and the next step shows how.
Numbers into text
str()turns a value into a string.str(12)is the text"12", so"Laps: " + str(laps)joins two strings, and it works.An f-string, from the Bill Splitter, does the same job without
+:f"Laps: {laps}". Use whichever reads better.Once numbers are strings,
+joins them side by side instead of adding them. Sostr(2) + str(3)is"23", not 5. That's the usual surprise, and it's the same thing that happens to the digits people type intoinput().Script three, the date
Neil's first model rocket went up on 14 March, and this script was meant to say so. Run it and read the last line of the traceback.
Fix it with
str()or an f-string, so it printsLaunch day, March 14. The check also tries other days, so the number has to come fromday, not be typed into the text.The right type, the wrong value
A ValueError means a value had the right type but couldn't be used.
int()takes a string, soint("seven")gives it the right type. But"seven"isn't written in digits, soint()can't read it, and the program stops. You met one in the Bill Splitter:int("2.5").A TypeError is the other way round: the type itself is wrong, like a number where
+needed a string.People mix the two up. Ask: was it the wrong kind of thing, or the right kind with the wrong contents? A ValueError often means something a person typed wasn't what the code expected.
Script four, the glider
Neil wrote this for the wings of a model glider. Run it, type
4.5, and read the last line of the traceback.Fix it so it prints
Wingspan in centimetres: 450.0. Whole numbers, like3, still have to work.Script five, the board
- NeilThis last script's the board itself, and there are two bugs hiding in this one.
The race-day board itself, with two mistakes in it. Python only reports the first problem it finds. Fix that one, run it again, and read the next.
When both are fixed, Neil gets his race-day board back:
Race day at the indoor track Practice 09:30 Race 16:00Ship it
- NeilPractice at nine thirty, race at four, which is exactly what I meant it to print back in my first term.
- PaulThat's five scripts and six tracebacks, and most people never read even one.