Rocket's program should print a number. He is not sure which number. "Let's just run it and see!" he says.
Raven opens her notebook. "What if we trace it first? We read one line at a time."
Nova draws a table in the air. "Write the value of x after every line." After that, they will hunt a sneaky bug. Let's help them trace the code.
TRACING is reading code one line at a time and writing what happens. It's the secret tool of every debugger. Trace this one:
x = 1
REPEAT 3 TIMES:
x = x + 2
print(x) | Step | What runs | x is now |
|---|---|---|
| start | x = 1 | ? |
| 1 | x = x + 2 | ? |
| 2 | x = x + 2 | ? |
| 3 | x = x + 2 | ? |
| end | print(x) | ? |
This program should say 'good morning' if it's morning, else 'good evening'.
But it always says 'good evening'. Why?
hour = 8
IF hour > 20:
print 'good morning'
ELSE:
print 'good evening' When code looks right but acts wrong, trace it. Write the value of each box after every line.
n = 2
REPEAT 3 TIMES:
n = n + 3
print(n) | Statement | True or false? |
|---|---|
| Tracing means following the code one line at a time. | ? |
| A trace table shows the value after each step. | ? |
| You can find every bug by reading only the first line. | ? |
| The symbol > means greater than. | ? |