Year 9 · Y9.3 Python Programming · Lesson 1

Stretch tasks Structuring a program

Task 1

Quiz Challenge

A quiz game against four computer players, then short and long written answers.

  1. Quiz Blitz12 questions against the clock
  2. Quick Fire4 short answers
  3. Big Think2 long answers

About 20 minutes

Task 2

This or That

Pixel the cat asks 8 questions. Each has two answers: pick the right one.

  • Two answers to every question
  • A fish treat for every right answer
  • Streaks win Pixel shades, then a party hat

About 4 minutes

Task 3

Memory Match

Flip the cards two at a time to match each term card to its meaning card.

  • 12 cards: each term has a meaning
  • One peek at every card
  • Combo points for pairs found in a row

6 pairs, about 4 minutes

The games need JavaScript. Every question is below, with the answers at the end.

Read the lesson: Structuring a program

Structuring a program: the questions

Quiz Challenge, round 1: Quiz Blitz

  1. What is printed?

    def grade_for(mark):
        if mark >= 70:
            return "Distinction"
        elif mark >= 50:
            return "Pass"
        else:
            return "Fail"
    
    print(grade_for(70), grade_for(49))
    • Pass Fail
    • Distinction Pass
    • Pass Pass
    • Distinction Fail
  2. What is the difference between a function and a procedure?

    • Procedures always run faster
    • A function returns a value; a procedure does not
    • A procedure returns a value; a function does not
    • Only functions can have parameters
  3. A local variable can be changed from anywhere in the program.

    • True
    • False
  4. What happens when this runs?

    def add_tax(amount):
        tax = amount * 0.2
        return amount + tax
    
    print(add_tax(50))
    print(tax)
    • It prints 60 and then 10
    • It prints nothing at all
    • It prints 60.0, then stops with a NameError
    • It prints 60.0 and then 10.0
  5. What is printed?

    def triple(n):
        return n * 3
    
    def add_one(n):
        return n + 1
    
    print(add_one(triple(4)))

  6. Which TWO are advantages of local variables?

    Tick 2.

    • They can be changed from anywhere
    • They make the program use more memory
    • Nothing outside the subroutine can change them by accident
    • The same name can be reused safely in different subroutines
    • They last for the whole program
  7. How many lines are printed?

    def greet(name, times):
        for i in range(times):
            print("Hi", name)
    
    greet("Sam", 2)
    • 2
    • 1
    • 3
    • 0
  8. The main program calls grade_for(65). Put what happens in order.

    Number these 1 to 4.

    • 65 >= 70 is checked: False
    • 65 >= 50 is checked: True
    • Pass is returned to the main program
    • grade_for(65) is called
  9. Why does the main program of a well-structured program read almost like English?

    • The details are hidden inside well-named subroutines
    • Python translates it into English
    • It does not use any variables
    • It is written entirely in comments
  10. What is printed?

    def mystery(a, b):
        if a > b:
            return a - b
        return b - a
    
    print(mystery(3, 10))

  11. A subroutine can be called many times with different parameters.

    • True
    • False
  12. What is printed?

    def first_even(numbers):
        for n in numbers:
            if n % 2 == 0:
                return n
        return -1
    
    print(first_even([3, 7, 8, 10]))
    • 10
    • -1
    • 3
    • 8

Quiz Challenge, round 2: Quick Fire

  1. Explain why local variables are usually better than global ones. [2 marks]

  2. Explain what return does in a function. [2 marks]

  3. Give two benefits of splitting a program into subroutines. [2 marks]

  4. What is printed, and why? [2 marks]

    def double(n):
        n = n * 2
    
    x = 5
    double(x)
    print(x)

Quiz Challenge, round 3: Big Think

  1. Explain how the subroutines in this program work together. [4 marks]

    def get_valid_mark(prompt):
        while True:
            entry = input(prompt)
            if entry.isdigit() and 0 <= int(entry) <= 100:
                return int(entry)
            print("Please enter a whole number between 0 and 100.")
    
    def grade_for(mark):
        if mark >= 70:
            return "Distinction"
        elif mark >= 50:
            return "Pass"
        else:
            return "Fail"
    
    mark = get_valid_mark("Enter the mark: ")
    print("Grade:", grade_for(mark))

  2. A 200-line game has all of its code in one long block, using global variables. Explain how you would restructure it, and why. [6 marks]

This or That: tick the right answer

  1. show_menu() prints a menu and returns nothing. What kind of subroutine is it?

    • A function
    • A procedure
  2. What is printed?

    def grade_for(mark):
        if mark >= 70:
            return "Distinction"
        elif mark >= 50:
            return "Pass"
        else:
            return "Fail"
    
    print(grade_for(70))
    • Distinction
    • Pass
  3. Where does a local variable exist?

    • Everywhere in the program
    • Only inside the subroutine that made it
  4. What is printed?

    def add_tax(amount):
        tax = amount * 0.2
        return amount + tax
    
    print(add_tax(10))
    • 12.0
    • 2.0
  5. What is printed?

    def f(x):
        return x + 1
    
    print(f(f(3)))
    • 5
    • 4
  6. In get_valid_mark, the loop is while True. Why does it ever stop?

    • return ends the subroutine once a mark is valid
    • It stops by itself after ten tries
  7. Which line runs a subroutine called show_menu?

    • show_menu()
    • def show_menu():
  8. What is printed?

    total = 0
    
    def add(n):
        total = n
    
    add(5)
    print(total)
    • 0
    • 5

Memory Match: match each term to its meaning

TermMeaning
A Subroutine1 A subroutine that returns a value
B Function2 A value passed in when a subroutine is called
C Procedure3 Exists only inside its subroutine
D Local variable4 A subroutine that returns nothing
E Global variable5 A named block of code you can call
F Parameter6 Exists everywhere in the program
Answers

Round 1: Quiz Blitz

  1. Distinction Fail 70 is at least 70, so Distinction. 49 is below 50, so the else branch returns Fail.
  2. A function returns a value; a procedure does not Both are subroutines. A function sends a value back with return; a procedure just does a job.
  3. False A local variable only exists inside the subroutine that created it.
  4. It prints 60.0, then stops with a NameError tax is local to add_tax. Once the function ends, tax no longer exists, so print(tax) fails.
  5. 13 The inner call runs first: triple(4) is 12, then add_one(12) is 13.
  6. Nothing outside the subroutine can change them by accident; The same name can be reused safely in different subroutines Local variables are sealed inside their subroutine, which prevents a whole family of bugs.
  7. 2 times is 2, so the loop prints Hi Sam twice.
  8. grade_for(65) is called → 65 >= 70 is checked: False → 65 >= 50 is checked: True → Pass is returned to the main program An if-elif chain checks each condition in turn and stops at the first one that is true.
  9. The details are hidden inside well-named subroutines get_valid_mark and grade_for say what they do, so the main program reads as a list of steps.
  10. 7 3 is not greater than 10, so it returns 10 - 3 = 7. The function always gives the gap between two numbers.
  11. True That is the point of a subroutine: write it once, call it whenever you need it.
  12. 8 return leaves the function at once, so it stops at the first even number and never reaches 10.

Round 2: Quick Fire

    • They can only be changed inside their own subroutine, so nothing else changes them by accident
    • Each subroutine is self-contained, so it is easier to test and debug, and names can be reused
    Model answer

    A local variable can only be changed inside its own subroutine, so no other part of the program can change it by accident. That keeps each subroutine self-contained, which makes it easier to test and debug.

    • It sends a value back to the code that called the function
    • It ends the function immediately
    Model answer

    return sends a value back to the code that called the function, and it ends the function immediately.

    • Code can be reused by calling it again
    • Each part can be tested on its own
    • The program is easier to read and understand
    • Different people can work on different parts
    • A bug can be fixed in one place
    Model answer

    Each subroutine can be reused by calling it whenever it is needed, and each one can be tested on its own before the whole program is put together.

    • 5
    • The function only changes its own local copy, and returns nothing
    Model answer

    5 is printed. Inside double, n is a local copy of the value, so doubling it does not change x, and the function does not return anything.

Round 3: Big Think

    • get_valid_mark keeps asking until a mark from 0 to 100 is entered
    • It returns the valid mark as a whole number
    • grade_for uses if, elif and else to choose the grade
    • The main program passes the mark into grade_for and prints the result
    • Each subroutine has one job, so the program is easy to read
    Model answer

    get_valid_mark uses a while True loop to keep asking until the user enters a whole number from 0 to 100, then returns it as an integer. grade_for takes the mark as a parameter and uses if, elif and else to return Distinction, Pass or Fail. The main program calls get_valid_mark, passes the result into grade_for and prints the grade. Each subroutine has one job, so the main program reads almost like English.

    • Split it into subroutines that each do one job
    • Pass data in with parameters and back out with return
    • Replace the globals with local variables
    • This stops variables being changed by accident, causing bugs
    • Each part can be tested on its own
    • The code is easier to read, maintain and reuse
    • Use meaningful names and comments
    Model answer

    I would split the code into subroutines that each do one job, such as show_menu, play_round and update_score, with meaningful names and comments. Data would be passed in through parameters and sent back with return, so the global variables could be replaced with local ones. That stops one part of the game changing a variable by accident and causing bugs that are hard to find. Each subroutine could then be tested on its own, and the whole program would be easier to read, maintain and reuse.

This or That

  1. A procedure A subroutine that does something without returning a value is a procedure.
  2. Distinction 70 >= 70 is true, so the first branch returns Distinction before the others are checked.
  3. Only inside the subroutine that made it A local variable disappears when its subroutine ends, so nothing else can change it by accident.
  4. 12.0 tax is 10 x 0.2 = 2.0, and the function returns 10 + 2.0 = 12.0.
  5. 5 f(3) is 4, and then f(4) is 5.
  6. return ends the subroutine once a mark is valid return leaves the subroutine straight away, so the loop ends as soon as a valid mark comes back.
  7. show_menu() def defines the subroutine. Writing its name with brackets calls it.
  8. 0 total inside add is a new local variable. The global total outside is still 0.

Memory Match

  • A 5 Subroutine: A named block of code you can call
  • B 1 Function: A subroutine that returns a value
  • C 4 Procedure: A subroutine that returns nothing
  • D 3 Local variable: Exists only inside its subroutine
  • E 6 Global variable: Exists everywhere in the program
  • F 2 Parameter: A value passed in when a subroutine is called