← VCE Software Development U3O1

Modifying an existing program

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.

Understanding Modifying an existing program in VCE Software Development U3O1

What it does: Editing a single value means that if the output changes or breaks, you know exactly which change caused it. A comment written after reading and tracing the code records what it does, so you can edit it safely. Even a small change can break something else. Re-running your test cases shows whether the program still works. Making one change at a time keeps cause and effect clear. With two changes, either one might be the culprit.

Common mistakes: 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. 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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 178–193) and word search (Modules 168–193).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
You are given a program written by someone else and need to change it. What should you do first?
Delete it and start again from scratchRead and trace the code to see what it does
Matches
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.
Applying
Meanings
Term
modify code
Meaning
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.
Practice
def discount(price):
Testing
Gaps
return n 2
Check Answer
Spots
2 gst = price * 0.1
3 return price - gst

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Stag' badge — Forest Legend, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

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
Modifying code starts with understanding it. Reading and tracing shows you what the program does before you change anything.
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
Changing one thing at a time means that if something breaks, you know exactly which change caused 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 ✓
Even a small change can break something else. Re-running your test cases shows whether the program still works.
4. True or false: You can safely change code you have not read, as long as the change looks simple.
A. False ✓
B. True
Without reading and tracing the code first, you can't predict what your change will do. A simple-looking change can break other parts.
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 ✓
Tracing a test call first shows you how the code behaves now (20), so you can compare it with the result after your change.

Match the Pairs

print(total) # trace → Add a print line to see what a variable holds while you read and trace someone else's code
assert area(3, 4) == 12 → Re-run a test case after your change to check the function still gives the right result
Change one thing at a time: only edit the 0.10 discount rate to 0.20, then run the program again → rate = 0.20
Reading someone else's code: explain what a function does before you edit it → # returns the price after discount
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.
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.

Key Term

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.

Fill in the Blanks

1. def double(n): return n * 2 assert double(4) == 8
Hint: Your test expects 8 from 4. Type the operator that makes the function double n.
2. def inc(n): return n + 1 assert inc(3) == 4
Hint: After changing code, you re-run a test. Type the keyword that checks a test case.
3. def area(w, h): return w * h assert area(2, 3) == 6
Hint: Type the name of the function that the test case is checking.
4. def adult(age): return age >= 18 assert adult(18) == True
Hint: The test fails for age 18. Type the comparison operator that includes 18 itself.
5. total = 0 for n in [1, 2, 3]: total += n
Hint: Trace the loop. Type the operator that adds each n onto the running total.

Spot the Error

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

Student with a class code? Join your class →