Kids Coding
Find the Bug
Read the error. Print a value. Change one thing. Run again.
KidsIntermediateFind the Bug
Bugs are normal
A bug is code that does not do what you meant. Python often tells you the line. Read it. Change one thing. Run again.
A broken program
This one uses the wrong check. It always says miss even when the guess is right. That is a logic bug: it runs, but it is wrong.
Wrong on purpose
secret = 7
guess = 7
if guess = secret:
print("Hit")
else:
print("Miss")That example uses = instead of ==. It should fail. Copy it, see the error, then fix it.
The fix
== not =
secret = 7
guess = 7
if guess == secret:
print("Hit")
else:
print("Miss")Print to see
If the answer looks wrong, print the variables. Do not guess what is in the boxes. Look.
- Read the error from the bottom.
- Fix one cause.
- Run again before you change something else.
Inspect
total = 2 + 2
print("total is", total)Real life
A homework total that came out wrong.
You do not rewrite the whole page. You print the middle step and see where it broke.
Pocket money check
had = 20
spent = 7
left = had - spent
print("I think I have", left)
if left != 13:
print("Check the sums again")
else:
print("That matches")Change one word. Run it. That is how this idea shows up outside the lesson.
Try it
Exercise 1: This line is wrong: print("Hi" + 3). Fix it so it prints Hi3.
Show solution
print("Hi" + str(3))