๐ L02 โ Loops + Functions Self-Check Handout
Class: Year 9 Game On ยท 9GAMZA ยท Term 2 2026
Duration: 75 minutes (target ~50 minutes; extra time for thinking)
Marks: /75 total ยท Part A 20 ยท Part B 15 ยท Part C 20 ยท Part D 20
What we're checking: for loops, while loops, list comprehensions NEW, functions (def + return + parameters), default values, *args NEW, scope.
Rules: Answer EVERY question to unlock Submit. Once submitted, you'll see your score + correct answers. Your work auto-saves on this laptop as you type. No looking things up โ only what you remember.
Part A โ Predict the Output
A1 (2 marks)
What prints?
for i in range(4):
print(i)
A2 (2 marks)
What prints?
for i in range(2, 6):
print(i)
A3 (2 marks)
What prints?
n = 0
while n < 3:
n += 1
print(n)
A4 (2 marks) LIST COMPREHENSION
What prints?
squares = [x * x for x in range(4)]
print(squares)
A5 (2 marks) LIST COMPREHENSION
What prints?
nums = [1, 2, 3, 4, 5, 6]
evens = [n for n in nums if n % 2 == 0]
print(evens)
A6 (2 marks)
What prints?
def greet(name):
return f"Hello, {name}!"
print(greet("Ada"))
A7 (2 marks)
What prints?
def add(a, b=10):
return a + b
print(add(5))
A8 (2 marks) *args
What prints?
def total(*scores):
return sum(scores)
print(total(10, 20, 30))
A9 (2 marks)
What prints?
def stats(hp, max_hp):
return hp, max_hp, hp / max_hp
result = stats(40, 100)
print(result)
A10 (2 marks) SCOPE
What happens?
def make_score():
score = 100
make_score()
print(score)
Part B โ Fill in the Blank
B1 (3 marks)
Fill in the blank to print every item in
fruits, one per line.fruits = ["apple", "banana", "cherry"]
_________________
print(fruit)
B2 (3 marks) LIST COMPREHENSION
Fill in the blank using a list comprehension to make a list of squares:
[0, 1, 4, 9, 16]._________________
print(squares) # [0, 1, 4, 9, 16]
B3 (3 marks)
Fill in the blank to define a function that doubles a number and returns the result.
_________________
return x * 2
print(double(7)) # 14
B4 (3 marks)
Fill in the blank โ a while loop that counts down from 5 to 1 (inclusive).
n = 5
_________________
print(n)
n -= 1
B5 (3 marks)
Fill in the blank โ define
greet with a default greeting of "Hello"._________________
print(f"{greeting}, {name}!")
greet("Sam") # Hello, Sam!
greet("Sam", "Hola") # Hola, Sam!
Part C โ Debug
C1 (5 marks)
This for-loop should print 1 to 5. Fix it. Write the corrected first line only.
for i in range(1, 5):
print(i)
# wanted: 1 2 3 4 5 / actual: 1 2 3 4
C2 (5 marks)
This function should return the bigger of two numbers, but it doesn't return anything. Fix the function body.
def bigger(a, b):
if a > b:
a
else:
b
print(bigger(5, 9)) # prints None โ should print 9
C3 (5 marks)
This while loop never stops. Find the bug โ write the line that needs adding (or fixing) inside the loop.
n = 0
while n < 5:
print(n)
# (this loop never ends!)
C4 (5 marks) SCOPE
This code should print the doubled value. Fix it โ write the corrected last line.
def double_it(x):
result = x * 2
double_it(7)
print(result) # NameError โ result doesn't exist out here
Part D โ Coding Tasks (Write Python)
D1 (10 marks)
Task: Write a function called
calculate_damage that takes hp, damage, and armour, and returns the new HP after the hit. Damage that gets through = damage - armour (but never below 0). The new HP is hp - damage_through.
Test cases your function must pass:
calculate_damage(100, 30, 10) โ 80 (30 dmg, 10 armour soaks โ 20 through โ 100โ20=80)calculate_damage(100, 5, 50) โ 100 (5 dmg, 50 armour soaks all โ 0 through โ 100โ0=100)calculate_damage(50, 70, 0) โ โ20 (70 dmg, no armour โ all through โ 50โ70=โ20)
D2 (10 marks) LIST COMPREHENSION + *args
Task: Write a function called
average that takes any number of scores using *args, and returns their average. Then use a list comprehension to make a list of "high" scores (โฅ 80) from a given list, and print the average of those high scores.
Expected behaviour:
Then for
average(80, 90, 100) โ 90.0Then for
scores = [55, 92, 78, 88, 100, 67, 85]:highs = [s for s in scores if s >= 80] โ [92, 88, 100, 85]average(*highs) โ 91.25 (or similar โ show what your code prints)
Once submitted, all answers lock and you'll see your score + correct answers.
๐ Your Results
0 / 75
0%
How to submit to Google Classroom:
1. Click Copy results for Classroom โ a summary copies to your clipboard.
2. Open today's L02 assignment in Google Classroom.
3. Paste (Ctrl+V) into the comment / submission box.
4. Click Mark as done.
1. Click Copy results for Classroom โ a summary copies to your clipboard.
2. Open today's L02 assignment in Google Classroom.
3. Paste (Ctrl+V) into the comment / submission box.
4. Click Mark as done.