Modifying an existing Python program

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

Modifying an existing Python program

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
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
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
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"
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

Match the Pairs

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

Left columnRight column
1lives = 3 → lives = 5 AChange a condition so the program needs a higher score before the message appears
2if score > 10: → if score > 20: BNo prediction was made. Predict the output first, then run it and compare, so you learn whether you understood the code.
3Change one thing at a time, then run the program to see what that change did Cprint("Hello, Sam!") → print("Hello, Mia!")
4Adding a feature means writing a new line that builds on the working program DToo 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.
5Changing the speed, the lives and the message all at once, then running the program Eprint("Score:", score) print("Lives:", lives)
6Editing the code and running it without thinking what the output should be FChange one value in a working program so the game gives the player more lives

Definitions

modify code

Fill in the Blanks

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

1.
# Change score so it starts at 10 score = __ print(score)
2.
age = 13 if age __ 13: print("Teen")
3.
# Change the loop to show 1 to 5 for i in range(1, _): print(i)
4.
total = 0 for n in [4, 7]: total __ n
5.
if score > 50: print("Pass") ____:

Spot the Error

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

Question 1
A score = 5
B bonus = 10
C score = score + Bonus
D print(score)
Question 2
A lives = 3
B print('Lives left:' lives)
C lives = lives - 1
Question 3
A name = input('Name? ')
B age = input('Age? ')
C age = int(age)
D print(name, 'is', age + '1')
Question 4
A total = 0
B for i in range(1, 6)
C total = total + i
D print(total)
Question 5
A speed = 5
B while speed < 20:
C speed = speed - 5
D print('Max speed')

Answer Key

Modifying an existing Python program
Section A: Quick Questions
Q1
A — Change lives = 3 to lives = 5
Q2
A — Predict what the new output will be
Q3
B — Change one thing, run it, then change the next
Q4
A — Change the condition to score >= 20
Q5
B — A message
Section B: Match the Pairs
Q1
lives = 3 → lives = 5 → Change one value in a working program so the game gives the player more lives
Q2
if score > 10: → if score > 20: → Change a condition so the program needs a higher score before the message appears
Q3
Change one thing at a time, then run the program to see what that change did → print("Hello, Sam!") → print("Hello, Mia!")
Q4
Adding a feature means writing a new line that builds on the working program → print("Score:", score) print("Lives:", lives)
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
# Change score so it starts at 10 score = 10 print(score)
Q2
age = 13 if age >= 13: print("Teen")
Q3
# Change the loop to show 1 to 5 for i in range(1, 6): print(i)
Q4
total = 0 for n in [4, 7]: total += n
Q5
if score > 50: print("Pass") else:
Section E: Spot the Error
Q1
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.
Q2
B — When you change a message, keep the comma between the arguments in print(). It should be print('Lives left:', lives).
Q3
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.
Q4
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):
Q5
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.