hands-on labIDE in your browserfree

Read the Error: Fix Five Broken Scripts from Their Tracebacks

Five of Bean There's scripts crash before opening. Read each traceback from the bottom up, find the line it points at and fix it: a NameError, a SyntaxError, a TypeError, an IndentationError and a script with two bugs in a row.

time
25 min
checked steps
5
level
Beginner
setup
None

Freeone of 170+ labs in Pro

free · no account needed to start

The job

7:45, fifteen minutes before opening. Five of Bean There's little scripts crash as soon as they start: the menu board, the specials sign, the cups counter, the opening-hours sign and the cup labels. Each one prints a traceback. Read it, fix the line it points at, and get the café ready in time.

5 steps, each checked when you finish it

A check runs your work at the end of every step. Hints and the full solution are there if you get stuck.

  1. 1

    The menu board: a NameError

    Press Run.

    read this step below ↓

  2. 2

    The specials sign: a SyntaxError

    Run specials.py.

  3. 3

    The cups counter: a TypeError

    The coffee machine's counter should say how many cups are left.

  4. 4

    The hours sign: an IndentationError

    Quick one.

  5. 5

    The cup labels: two bugs, one of them quiet

    The last script prints the labels that go on cups.

The lab, step by step

The free lab’s own text, every step of it. Hints and solutions stay inside the lab.

Step 1: The menu board: a NameError

Press Run. The menu board crashes, and Python prints a traceback: its account of what it was doing when it stopped. This one has four parts. Read them from the bottom up:

Traceback (most recent call last):
  File "/home/labuser/menu.py", line 6, in <module>
    print(f"Flat white: £{pirce // 100}.{pirce % 100:02d}")
                          ^^^^^
NameError: name 'pirce' is not defined. Did you mean: 'price'?
  1. The last line names the error and what went wrong. A NameError means the script used a name Python has never seen. Python even guesses what you meant.
  2. The arrows ^^^^^ sit under the exact spot on the line.
  3. The line above them is the code Python was running.
  4. line 6 says where that code lives in menu.py.

Go to line 6 of menu.py, fix the name, and press Run until the board prints:

Menu
Flat white: £3.20

Then press Check. Most bugs are found exactly like this: last line first, then the line number.

menu.py, the file you edit6 lines
# The menu board. It runs at opening time and prints today's prices (in whole pennies).
flat_white = 320
price = flat_white

print("Menu")
print(f"Flat white: £{pirce // 100}.{pirce % 100:02d}")

Step 2: The specials sign: a SyntaxError

Run specials.py. This time the error has no Traceback (most recent call last): line at the top:

  File "/home/labuser/specials.py", line 2
    special = "Pumpkin spice latte
              ^
SyntaxError: unterminated string literal (detected at line 2)

That's how you tell a SyntaxError: Python reads the whole file before it runs any of it, and it couldn't read this one, so not a single line ran. The message says a string was never closed: unterminated means it has a start and no end.

Fix line 2 and run it until the sign prints:

Today's special: Pumpkin spice latte

The caret ^ points at where Python's reading went wrong, which isn't always where the fix goes. Here it points at the opening quote, and the missing one is at the end of the line.

specials.py, the file you edit3 lines
# The specials sign by the door.
special = "Pumpkin spice latte
print(f"Today's special: {special}")

Step 3: The cups counter: a TypeError

The coffee machine's counter should say how many cups are left. Run cups.py, and start reading from the last line:

TypeError: can only concatenate str (not "int") to str

A TypeError means an operation got the wrong type of thing. + between two strings joins them ("concatenate" means join); between two numbers it adds. Between a string and a number Python won't guess which you meant, so it stops. The line above the message shows where: "Cups left: " + cups_left, where cups_left is the number 24.

Make it print:

Cups left: 24

An f-string is the usual fix, f"Cups left: {cups_left}", because it turns whatever is in the braces into text. str(cups_left) turns a number into text too.

cups.py, the file you edit3 lines
# The coffee machine's counter.
cups_left = 24
print("Cups left: " + cups_left)

Step 4: The hours sign: an IndentationError

Quick one. Run hours.py:

  File "/home/labuser/hours.py", line 3
    closes = "18:00"
IndentationError: unexpected indent

Python reads the spaces at the start of a line. A line that starts further in than the one before tells Python "this belongs inside the line above", and here there's nothing for it to belong to. From Level 2 you'll indent lines on purpose, under an if. For now, every line starts at the left edge.

Fix it so the sign prints:

Open 7:30 to 18:00
hours.py, the file you edit4 lines
# The opening-hours sign.
opens = "7:30"
 closes = "18:00"
print(f"Open {opens} to {closes}")

Step 5: The cup labels: two bugs, one of them quiet

The last script prints the labels that go on cups. Sam left a note taped to the printer:

Name in capitals, so we can read it across the counter. Size with a capital first letter. Sam Rivera's two large flat whites: SAM RIVERA | Large x 2

Run labels.py. It crashes with an AttributeError: the script asked a string for a method it doesn't have. Read the last line, Python suggests the name it meant, and fix it.

Then run it again. It runs this time and prints a label, but compare it with Sam's note. This second bug is the quiet kind: no traceback, just the wrong output. Python stops at the first error it meets, and a script that runs isn't the same as a script that's right. Make the label match the note:

SAM RIVERA | Large x 2

.upper() puts every letter in capitals, .title() gives each word a capital first letter and .capitalize() gives only the first letter of the text one. That's the last broken script: the café opens on time. Opening Morning is next, Level 1's boss.

labels.py, the file you edit7 lines
# The cup-label printer: a name and a size on every cup.
name = "  sam rivera"
size = "large"
cups = 2

label = f"{name.strip().titel()} | {size.upper()} x {cups}"
print(label)

what you get

  • An editor and terminal in your browser
  • A check at the end of every step
  • Hints and the full solution
  • Nothing to install or configure

before you start

  • Ask the Customer: input(), int() and f-strings

Part of the Learn Python track

Lab 4 of 15. Learn Python by running a café till: receipts, orders, discounts and the Friday rush. Nothing to install.

Questions about this lab

How do I read a Python traceback?

Start at the last line: it names the error and says what went wrong. The lines above say which file and line Python was running when it stopped.

Do I need to install anything?

No. Python runs right in your browser.

What you do in this lab

You read Python tracebacks the way developers do, from the last line up: the error's type, its message, the file and the line number. Each step is a real script with one kind of error to fix, NameError, SyntaxError, TypeError and IndentationError, and the last one hides a second bug behind the first.