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 program is paused at a breakpoint on the line total = add_up(prices). You want to run the whole add_up() call as one step and stay in the current function. Which do you use?
A Step over
B Step into
2. You are paused on a line that calls calculate_tax(), and you suspect the mistake is inside that function. Which do you use?
A Step over
B Step into
3. You have stepped into a function and checked it. The rest of it looks fine. Which do you use to finish it and return to the code that called it?
A Step into
B Step out
4. What is the job of the watch panel (variables panel) while you step through code?
A It shows the current value of variables so you can see them change after each line
B It automatically fixes any line where a variable has the wrong value
5. You step through a loop and watch the variable count. It should be 3 after the third line runs, but the watch panel shows 5. What is the best conclusion?
A The error happened on or before this line, so look at the last line that changed count
B The error must be on the last line of the program

Match the Pairs

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

Left columnRight column
1Step Over AContinue (F5)
2Step Into BMoves into the function being called on the current line and pauses on that function's first line.
3A watch window shows the current value of a variable at each pause, so you can see when it changes. CStep into on a built-in function like print() goes into library code you don't need. Use step over, then check total in the watch panel.
4Continue runs the program from a pause until it reaches the next breakpoint, skipping the lines in between. DRuns the current line in full, including any function it calls, then pauses on the next line in the same function.
5Step Into on print(total) to check why total is wrong EWatch: total = 45
6Stepping through 500 loop passes with Step Over, one line at a time, to reach the last pass FStepping line by line through every pass is slow. Set a breakpoint after the loop and use Continue, or watch the variable to find the line where its value goes wrong.

Definitions

step through

Fill in the Blanks

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

1.
total = 0 __________() total = total + 5
2.
total = total + 5 (Pdb) ____
3.
total = add(2, 3) (Pdb) ____
4.
(Pdb) _______ total
5.
(Pdb) ________

Spot the Error

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

Question 1
A price = 20
B discount = price / 0.1
C final = price - discount
D print(final)
Question 2
A total = 0
B for n in range(1, 4):
C total += n
D print(n, totl)
Question 3
A width = 4
B height = 3
C area = width + height
D print(area)
Question 4
A score = '0'
B for i in range(3):
C score += 5
D print(score)
Question 5
A def double(n):
B return n + 2
C x = double(4)
D print(x)

Answer Key

Stepping through code and watching variables
Section A: Quick Questions
Q1
A — Step over
Q2
B — Step into
Q3
B — Step out
Q4
A — It shows the current value of variables so you can see them change after each line
Q5
A — The error happened on or before this line, so look at the last line that changed count
Section B: Match the Pairs
Q1
Step Over → Runs the current line in full, including any function it calls, then pauses on the next line in the same function.
Q2
Step Into → Moves into the function being called on the current line and pauses on that function's first line.
Q3
A watch window shows the current value of a variable at each pause, so you can see when it changes. → Watch: total = 45
Q4
Continue runs the program from a pause until it reaches the next breakpoint, skipping the lines in between. → Continue (F5)
Q5
Step Into on print(total) to check why total is wrong → Step into on a built-in function like print() goes into library code you don't need. Use step over, then check total in the watch panel.
Q6
Stepping through 500 loop passes with Step Over, one line at a time, to reach the last pass → Stepping line by line through every pass is slow. Set a breakpoint after the loop and use Continue, or watch the variable to find the line where its value goes wrong.
Section C: Definitions
Q1
step through — Run paused code one line at a time using step over, step into or step out, while the watch panel shows each variable change, to find where a value goes wrong.
Section D: Fill in the Blanks
Q1
total = 0 breakpoint() total = total + 5
Q2
total = total + 5 (Pdb) next
Q3
total = add(2, 3) (Pdb) step
Q4
(Pdb) display total
Q5
(Pdb) continue
Section E: Spot the Error
Q1
B — Stepping over this line makes the watch panel show discount as 200, not 2. A 10% discount needs price * 0.1, not price / 0.1.
Q2
D — The watch panel lists a variable called total, not totl. Stepping onto this line raises a NameError. It should be print(n, total).
Q3
C — After stepping over this line, the watch window shows area as 7 when it should be 12. The area of a rectangle uses width * height, not +.
Q4
A — The watch panel would show score as the string '0'. Stepping over line 3 then fails because you cannot add an integer to a string. It should be score = 0.
Q5
B — Step into double() and watch n is 4, but the returned value is 6, not 8. Doubling needs n * 2, not n + 2.