Stepping through code and watching variables

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Stepping through code and watching variables

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A breakpoint has paused your program. What does 'step over' do?
A Runs the next line only, then pauses again
B Runs the rest of the program without stopping
2. After each step, where do you look to see how variable values have changed?
A The file name at the top of the editor
B The variable watch panel
3. What are you trying to find while you step through the code?
A The first line that gives a wrong variable value
B The last line in the program
4. Before running line total = total + price, total is 10 and price is 5. The watch panel then shows total as 5. What does this tell you?
A That line is faulty, because total should now be 15
B That line is fine, because the value changed
5. Which approach finds a logic error more precisely?
A Run the whole program and guess which line is wrong
B Step through one line at a time and check the watch panel

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1step over AThe variable watch panel shows that total currently holds the value 15 at this paused line
2watch: total = 15 BThe faulty line is the first line where the watch value differs from the trace table, not the last line. Find the earliest mismatch.
3After pausing at a breakpoint, step over one line and check the watch panel to see if the value changed as expected Ctotal = total + price
4Compare the watch panel value with the trace table row for the same step to find the first line that differs DOnly checking variables at the end hides which line caused the error. Step over one line at a time and check the watch panel after each step.
5Run the whole program, then check the watch panel once at the end ETrace: count = 3, Watch: count = 4
6Watch shows total = 12, trace table says 10, so the error is in the last line of the program FRuns the next line only, then pauses again so you can check the variables

Definitions

step over

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
total = 0 __________() total = total + 5
2.
total = 0 total = total _ 5 print(total)
3.
total = 0 for i in _____(1, 4): total += i
4.
total = 45 average = total _ 3 print(average)
5.
if score __ 50: print("Pass")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A total = 0
B count = 1
C for mark in [70, 85, 90]:
D total = total + mark
E count = count + 1
F print(total / count)
Question 2
A lives = 3
B score = 0
C while lives > 0:
D lives = lives + 1
E score = score + 10
F print(score)
Question 3
A price = '20.00'
B discount = 0.25
C sale = price - price * discount
D print(sale)
Question 4
A a = 5
B b = 9
C temp = a
D b = a
E b = temp
F print(a, b)
Question 5
A age = int(input('Age: '))
B fee = 10
C if age > 18:
D fee = 5
E print(fee)

Answer Key

Stepping through code and watching variables
Section A: Quick Questions
Q1
A — Runs the next line only, then pauses again
Q2
B — The variable watch panel
Q3
A — The first line that gives a wrong variable value
Q4
A — That line is faulty, because total should now be 15
Q5
B — Step through one line at a time and check the watch panel
Section B: Match the Pairs
Q1
step over → Runs the next line only, then pauses again so you can check the variables
Q2
watch: total = 15 → The variable watch panel shows that total currently holds the value 15 at this paused line
Q3
After pausing at a breakpoint, step over one line and check the watch panel to see if the value changed as expected → total = total + price
Q4
Compare the watch panel value with the trace table row for the same step to find the first line that differs → Trace: count = 3, Watch: count = 4
Q5
Run the whole program, then check the watch panel once at the end → Only checking variables at the end hides which line caused the error. Step over one line at a time and check the watch panel after each step.
Q6
Watch shows total = 12, trace table says 10, so the error is in the last line of the program → The faulty line is the first line where the watch value differs from the trace table, not the last line. Find the earliest mismatch.
Section C: Definitions
Q1
step over — Run the next line only, then pause again. Check the variable watch panel after each step to see which line first gives a wrong value.
Section D: Fill in the Blanks
Q1
total = 0 breakpoint() total = total + 5
Q2
total = 0 total = total + 5 print(total)
Q3
total = 0 for i in range(1, 4): total += i
Q4
total = 45 average = total / 3 print(average)
Q5
if score >= 50: print("Pass")
Section E: Spot the Error
Q1
B — count should start at 0. When you step over this line, the watch panel shows count as 1 before any marks are processed. It ends at 4 and the average is wrong. The line should be count = 0.
Q2
D — Stepping through shows lives going up (4, 5, 6...) instead of down, so the loop never ends. The line should be lives = lives - 1.
Q3
A — The quotes make price a string, and the watch panel shows it as text. Line 3 then fails because you cannot multiply a string by a number. It should be price = 20.00.
Q4
D — The swap breaks here. Stepping over this line shows a and b both holding 5, and the old value of b is lost. It should be a = b so that b = temp can then finish the swap.
Q5
C — The condition is reversed. When you step over it with age 25, the program runs the discount line and the watch panel shows fee as 5 for an adult. It should be if age < 18:.