Modifying an existing program

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

Modifying an existing program

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. You are given a program written by someone else and need to change it. What should you do first?
A Read and trace the code to see what it does
B Delete it and start again from scratch
2. Which approach to modifying a program is better?
A Change one small part, then re-run it
B Change five parts at once, then run it
3. After you change a line of code, what should you do next?
A Assume it works because the change was small
B Re-run your test cases to check the effect
4. True or false: You can safely change code you have not read, as long as the change looks simple.
A False
B True
5. A teammate's program has this function: def area(w, h): return w * h. You want it to also work for triangles. Which is the best first step?
A Rewrite the whole function using a new formula
B Trace area(4, 5) by hand to see what it returns

Match the Pairs

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

Left columnRight column
1print(total) # trace ARe-run a test case after your change to check the function still gives the right result
2assert area(3, 4) == 12 BAdd a print line to see what a variable holds while you read and trace someone else's code
3Change one thing at a time: only edit the 0.10 discount rate to 0.20, then run the program again CYou must read and trace the code first, then re-run your test cases so you can see the effect of the change.
4Reading someone else's code: explain what a function does before you edit it Drate = 0.20
5Changed 5 lines of the program, then ran it for the first time EToo many changes at once. If it breaks, you cannot tell which change caused the error. Change one thing, then re-run.
6Edited the code without reading it first, and skipped the test cases F# returns the price after discount

Definitions

modify code

Fill in the Blanks

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

1.
def double(n): return n _ 2 assert double(4) == 8
2.
def inc(n): return n + 1 ______ inc(3) == 4
3.
def area(w, h): return w * h assert ____(2, 3) == 6
4.
def adult(age): return age __ 18 assert adult(18) == True
5.
total = 0 for n in [1, 2, 3]: total __ n

Spot the Error

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

Question 1
A def add_gst(price):
B gst = price * 0.1
C return price - gst
D print(add_gst(100))
Question 2
A scores = [5, 8, 10]
B total = 1
C for s in scores:
D total += s
E print(total / len(scores))
Question 3
A def is_adult(age):
B if age >= 18:
C return True
D else
E return False
Question 4
A define double(n):
B return n * 2
C print(double(4))
D print(double(0))
Question 5
A names = ['Ava', 'Ben']
B names.append('Cal')
C print(names[3])
D print(len(names))

Answer Key

Modifying an existing program
Section A: Quick Questions
Q1
A — Read and trace the code to see what it does
Q2
A — Change one small part, then re-run it
Q3
B — Re-run your test cases to check the effect
Q4
A — False
Q5
B — Trace area(4, 5) by hand to see what it returns
Section B: Match the Pairs
Q1
print(total) # trace → Add a print line to see what a variable holds while you read and trace someone else's code
Q2
assert area(3, 4) == 12 → Re-run a test case after your change to check the function still gives the right result
Q3
Change one thing at a time: only edit the 0.10 discount rate to 0.20, then run the program again → rate = 0.20
Q4
Reading someone else's code: explain what a function does before you edit it → # returns the price after discount
Q5
Changed 5 lines of the program, then ran it for the first time → Too many changes at once. If it breaks, you cannot tell which change caused the error. Change one thing, then re-run.
Q6
Edited the code without reading it first, and skipped the test cases → You must read and trace the code first, then re-run your test cases so you can see the effect of the change.
Section C: Definitions
Q1
modify code — Read and trace the code first to see what it does, then change one small part at a time and re-run your test cases to check the effect of that change.
Section D: Fill in the Blanks
Q1
def double(n): return n * 2 assert double(4) == 8
Q2
def inc(n): return n + 1 assert inc(3) == 4
Q3
def area(w, h): return w * h assert area(2, 3) == 6
Q4
def adult(age): return age >= 18 assert adult(18) == True
Q5
total = 0 for n in [1, 2, 3]: total += n
Section E: Spot the Error
Q1
C — The change was meant to ADD 10% GST, so add_gst(100) should give 110. Subtracting gives 90. It should be 'return price + gst'. Re-run the test case after each change to catch this.
Q2
B — A running total must start at 0. Starting at 1 makes every result too big by 1. It should be 'total = 0'. Tracing the code by hand would show this.
Q3
D — The else line is missing its colon. Python needs 'else:' to start the block. Fix it and re-run to check that nothing else broke.
Q4
A — Python functions start with the keyword 'def', not 'define'. The line should be 'def double(n):'. Read the existing code carefully before changing it.
Q5
C — List positions start at 0, so 'Cal' is at index 2. Index 3 does not exist and causes an IndexError. It should be 'print(names[2])'.