←Previous page

08BUGS · CODE

Bugs in Python code.

Now the bugs hide in real Python. Some make Python stop. Others hide and give the wrong result.

TWO KINDS OF BUGS

Loud bugs and sneaky bugs.

Loud bugs

Python stops and shows an error. Good news: it tells you the line!

print("hi)

Sneaky bugs

The code runs, but the result is wrong. Python can't tell. You compare the result with what you wanted.

print(2 + 3) # wanted 2 * 3

Everyone makes bugs

Even the best programmers write bugs every day. A bug is not a failure. It is a clue.

  1. LookWhat did you want? What happened instead?
  2. Read the clueIf Python shows an error, read the last line and the line number.
  3. Fix one thingChange one small thing at a time.
  4. Run againIs the bug gone? If not, back to step 1.

RUN IT

Meet a loud bug.

Guess first. Then run real Python and check.

1

Guess the result

name = "PyBot" print("Hello!") print(nme)

What will Python show?

bug.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Run it and read PyBot's hint. Then fix line 3 and run again.

YOUR TURN

Find the bug!

Every card hides a bug. Some are loud, some are sneaky. A wrong answer becomes something to review.

0Completed
11Not tried
0Review
01

What is a bug?

PyBot's code says "hi" when it should say "bye".

What is the bug here?

02

Tap the buggy line

Python says: SyntaxError on line 2.

Which line has the bug?

03

Read the clue

score = 10 print(scroe)
line 2
NameError: name 'scroe' is not defined

What is the clue telling you?

04

A sneaky bug

PyBot wanted to jump 3 times. It jumped only 2.

for turn in range(2): print("jump")

Where is the sneaky bug?

05

Text or math?

PyBot wanted to see 4. Python showed 2 + 2.

print("2 + 2")

Why didn't PyBot see 4?

06

Pick the right fix

if light = "green": print("go")

Which first line squashes the bug?

07

The wrong snack

PyBot wanted the first snack, apple. It got cookie.

Tap the line with the sneaky bug.

08

Squash a loud bug

Run it. Read the clue. Fix the bug and match the goal.

Goal output
apple
cookie
grape
Yum!
OUTPUT

09

Catch a sneaky bug

No error, but the total is wrong. PyBot wanted 4 + 9 + 2.

Goal output
15
OUTPUT

10

Boss level: two bugs!

This box hides two bugs. Fix one, run again, then hunt the next one.

Goal output
Hi, Ana
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

A bug is a clue, not a failure.

Look, read the clue, fix one thing, run again. That is how every programmer in the world works.