← Years 7–8 Creating Digital Solutions

Modifying an existing Python program

Read a working program, then change one part, like a value, condition or message, to get new behaviour. Predict the result first, change one thing at a time, then run it.

Understanding Modifying an existing Python program in Years 7–8 Creating Digital Solutions

What it does: Only the name in the message was changed, so if the output is different you know exactly which edit caused it. The second print() line is a new feature added to a program that already worked, so it now shows lives as well as score. Modifying code means changing one part of a working program, like a value, to get new behaviour. Predicting first helps you check that you understand the code. If the result is different, you learn something. Changing one thing at a time makes it easy to find which change caused a problem. The condition decides when the code runs. Changing the condition changes when the player wins. Changing the message only changes the text.

Common mistakes: Python is case-sensitive. The variable is called bonus, not Bonus, so this causes a NameError. When you change code, keep variable names exactly the same: score = score + bonus. When you change a message, keep the comma between the arguments in print(). It should be print('Lives left:', lives).

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 50–125) and word search (Modules 3–125).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A program has the line lives = 3. You want the player to start with 5 lives. What is the best change?
Delete the whole program and start againChange lives = 3 to lives = 5
Matches
Changing the speed, the lives and the message all at once, then running the program
↓
Too many changes at once. If the program breaks or acts strangely, you cannot tell which change caused it. Change one thing, run, then change the next.
Applying
Meanings
Term
modify code
Meaning
Read a working program, then change one part, like a value, condition or message, to get new behaviour. Predict the result first, change one thing at a time, then run it.
Practice
lives = 5
Testing
Gaps
score =
Check Answer
Spots
2bonus = 10
3score = score + Bonus

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

The 'Dolphin' badge — Reef Streak, 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. A program has the line lives = 3. You want the player to start with 5 lives. What is the best change?
A. Change lives = 3 to lives = 5 ✓
B. Delete the whole program and start again
Modifying code means changing one part of a working program, like a value, to get new behaviour.
2. Before you run your changed program, what should you do first?
A. Predict what the new output will be ✓
B. Run it and hope for the best
Predicting first helps you check that you understand the code. If the result is different, you learn something.
3. You want to change a program in three ways: the speed, the message and the score. Which is the better plan?
A. Change all three at once, then run it
B. Change one thing, run it, then change the next ✓
Changing one thing at a time makes it easy to find which change caused a problem.
4. The code is: if score > 10: print("Win"). You want players to win with a score of 20 or more. Which change works?
A. Change the condition to score >= 20 ✓
B. Change "Win" to "20"
The condition decides when the code runs. Changing the condition changes when the player wins. Changing the message only changes the text.
5. A program prints print("Hello, Sam!"). You change it to print("Hello, Mia!"). What kind of part did you change?
A. A condition
B. A message ✓
The text inside the quotes is the message. Changing it changes what is shown, but not how the program makes decisions.

Match the Pairs

lives = 3 → lives = 5 → Change one value in a working program so the game gives the player more lives
if score > 10: → if score > 20: → Change a condition so the program needs a higher score before the message appears
Change one thing at a time, then run the program to see what that change did → print("Hello, Sam!") → print("Hello, Mia!")
Adding a feature means writing a new line that builds on the working program → print("Score:", score) print("Lives:", lives)
Changing the speed, the lives and the message all at once, then running the program → Too many changes at once. If the program breaks or acts strangely, you cannot tell which change caused it. Change one thing, run, then change the next.
Editing the code and running it without thinking what the output should be → No prediction was made. Predict the output first, then run it and compare, so you learn whether you understood the code.

Key Term

modify code
Read a working program, then change one part, like a value, condition or message, to get new behaviour. Predict the result first, change one thing at a time, then run it.

Fill in the Blanks

1. # Change score so it starts at 10 score = 10 print(score)
Hint: Type the new starting value that the comment asks for.
2. age = 13 if age >= 13: print("Teen")
Hint: Type the comparison operator that lets 13 pass, as well as anything higher.
3. # Change the loop to show 1 to 5 for i in range(1, 6): print(i)
Hint: Type the stop number for range(). Remember range stops just before it.
4. total = 0 for n in [4, 7]: total += n
Hint: Add a running total feature by typing the operator that adds n onto total.
5. if score > 50: print("Pass") else:
Hint: Add a new feature by typing the keyword that gives a fallback branch.

Spot the Error

Question 1
A score = 5
B bonus = 10
C score = score + Bonus
D print(score)
Line C — Python is case-sensitive. The variable is called bonus, not Bonus, so this causes a NameError. When you change code, keep variable names exactly the same: score = score + bonus.
Question 2
A lives = 3
B print('Lives left:' lives)
C lives = lives - 1
Line B — When you change a message, keep the comma between the arguments in print(). It should be print('Lives left:', lives).
Question 3
A name = input('Name? ')
B age = input('Age? ')
C age = int(age)
D print(name, 'is', age + '1')
Line D — age was converted to an int, so you can't add the string '1' to it. This causes a TypeError. Use the number 1: age + 1.
Question 4
A total = 0
B for i in range(1, 6)
C total = total + i
D print(total)
Line B — When you change the range values for a new result, keep the colon at the end of the for line. It should be for i in range(1, 6):
Question 5
A speed = 5
B while speed < 20:
C speed = speed - 5
D print('Max speed')
Line C — Changing + to - means speed gets smaller and never reaches 20, so the loop never ends. It should be speed = speed + 5 so the loop stops.

Student with a class code? Join your class →