← Back to course
Code Crew G3 / Week 06 / Thursday
4/6
Week 06 Β· Debugging

Thursday

// Finding the broken step

Thursday: Read & Trace

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)
StepWhat runsx is now
startx = 1?
1x = x + 2?
2x = x + 2?
3x = x + 2?
endprint(x)?
What does it print?
WHY THIS EXERCISETracing (running code line-by-line on paper) is a key debugging skill.

Bug Detective

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'
The bug is:
Fix:

Trace It Yourself

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)
After lap 1, what is n?
After lap 2, what is n?
What does print(n) show at the end?
FIX THE GREETING
  • Read the question.
  • Tap your answer.
hour = 8. The code asks IF hour > 20. Is 8 greater than 20?
So what does that buggy program print?
Which question would fix it?
After you change the code, what should you do?
StatementTrue 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.?
WHY THIS EXERCISETracing is the secret tool of every debugger.
x = 10. Then x = x - 3. Then x = x - 3. Then print(x). What does it print?
WHY THIS EXERCISEEach line changes the box. The last value is what prints.
Try it
Write a three line program with a box named k.
Trace it on paper, line by line.
Ask a grown-up to check your trace.
← Wednesday