Year 8 · Y8.3 Python: Loops, Lists and Functions · Lesson 3
Stretch tasks Functions and validation
Quiz Challenge
A quiz game against four computer players, then short and long written answers.
- Quiz Blitz12 questions against the clock
- Quick Fire4 short answers
- Big Think2 long answers
About 20 minutes
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
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: Functions and validation
Functions and validation: the questions
Quiz Challenge, round 1: Quiz Blitz
What is printed?
def double(n): return n * 2 print(double(7) + 1)- 14
- 16
- 8
- 15
In def greet(name): what is name?
- A loop
- A list
- A parameter
- A return value
Every function must return a value.
- True
- False
Which TWO are real benefits of functions?
Tick 2.
- A bug can be fixed in one place
- Programs always run faster
- Variables no longer need names
- Input no longer needs validating
- Code is written once and used many times
Which check makes sure an age is between 11 and 18?
- Range check
- Presence check
- Type check
- Length check
A password must have at least 8 characters. Which check is this?
- Length check
- Range check
- Presence check
- Type check
What is printed?
def area(w, h): return w * h print(area(3, 4) + area(2, 5))The user types 130, then -2, then 45. How many times is Try again shown?
age = int(input("Age: ")) while age < 0 or age > 120: age = int(input("Try again: "))- 3
- 0
- 2
- 1
Validation can prove that the data someone enters is correct.
- True
- False
Put the lines of this program in order.
Number these 1 to 4.
total = a + b(indented)return total(indented)print(add(2, 3))def add(a, b):
What does "42".isdigit() return?
- An error
- True
- False
- 42
What is printed on the second line?
def shout(word): print(word.upper()) result = shout("hi") print(result)- HI
- shout
- An error
- None
Quiz Challenge, round 2: Quick Fire
Explain the difference between a parameter and a return value. [2 marks]
Give two reasons why programmers use functions. [2 marks]
A form asks for a UK postcode. Name a suitable validation check, and say what it checks. [2 marks]
Explain why validation cannot guarantee that data is correct. [2 marks]
Quiz Challenge, round 3: Big Think
Explain how this code makes sure the program does not stop with an error when the user types a word instead of a number. [4 marks]
age = input("Age: ") while not age.isdigit(): print("Numbers only, please.") age = input("Age: ") age = int(age)A game asks the player for a whole number from one up to ten, in five different places. Explain how a function with validation would improve the program. [6 marks]
This or That: tick the right answer
Which keyword starts a function definition in Python?
functiondef
What is printed?
def triple(n): return n * 3 print(triple(4))- 12
- 7
What does return do?
- Shows the value on the screen
- Sends a value back to the code that called it
A bug sits in code that was copied into ten places. What would a function have saved?
- Fixing it once instead of ten times
- Adding a comment in ten places
A form makes sure the name box is not empty. Which check is this?
- Presence check
- Length check
A quantity box rejects ten but accepts 10. Which check is this?
- Type check
- Range check
Why use a while loop for validation?
- It keeps asking until the input is acceptable
- It runs exactly once, which is quicker
What is printed?
print("12a".isdigit())- False
- True
Memory Match: match each term to its meaning
| Term | Meaning |
|---|---|
A def | 1 Sends an answer back from a function |
| B Parameter | 2 Something has been entered |
C return | 3 The value lies between two limits |
| D Presence check | 4 Starts a function definition |
| E Range check | 5 The right number of characters |
| F Length check | 6 A value a function needs, named in its brackets |
Answers
Round 1: Quiz Blitz
- 15 double(7) returns 14, then 1 is added outside the function.
- A parameter A parameter is a value the function needs, passed in when it is called.
- False Some functions just do something, such as printing a menu, and return nothing.
- Code is written once and used many times; A bug can be fixed in one place Functions are about reuse and organisation. They do not make code faster or remove the need for checks.
- Range check A range check makes sure a value lies between two limits.
- Length check Counting characters is a length check.
- 22 area(3, 4) is 12 and area(2, 5) is 10: 22 in total.
- 2 130 is too big and -2 is too small, so it asks again twice. 45 is valid, so the loop ends.
- False Validation only checks data is sensible. A wrong birthday can still be a perfectly valid date.
def add(a, b):→total = a + b(indented) →return total(indented) →print(add(2, 3))Define the function first, with its body indented, then call it.- True isdigit returns True when every character in the string is a digit.
- None shout prints HI but returns nothing. A function with no return gives back None.
Round 2: Quick Fire
- A parameter is a value passed into a function
- A return value is the result sent back out of the function
Model answerA parameter is a value passed into a function so it can do its work. A return value is the result the function sends back to the code that called it.
- Code is written once and reused
- A bug only needs fixing in one place
- Programs are shorter and easier to read
- Each function can be tested on its own
Model answerCode is written once and reused many times, and if there is a bug it only needs fixing in one place.
- Names a suitable check
- Says what it checks
Model answerA length check, which checks the postcode has between 5 and 8 characters. A presence check would also make sure it has not been left blank.
- It only checks that data is sensible or allowed
- A wrong value can still pass, such as a mistyped birthday that is still a real date
Model answerValidation only checks that data is sensible and follows the rules. A wrong value can still pass: if someone mistypes their birthday as another real date, it is valid but still wrong.
Round 3: Big Think
- isdigit checks that every character is a digit
- The while loop repeats until the input is valid
- A message tells the user what went wrong
- int() is only reached once the input is all digits, so it cannot fail
Model answerisdigit checks whether every character the user typed is a digit. While it is not, the loop prints a message telling the user to type numbers only, and asks again. The loop only ends when the input is all digits, so int(age) is only reached when the conversion is safe and cannot cause a ValueError.
- The asking and checking code is written once and called five times
- Parameters, such as low and high, let it be reused for other ranges
- It returns the valid number to wherever it was called
- A range check rejects numbers below 1 or above 10
- A type check, such as isdigit, stops letters causing an error
- A loop keeps asking until the input is valid
- A bug only has to be fixed in one place
- The program is shorter and easier to read
Model answerInstead of copying the same input code five times, a function such as get_number(low, high) is written once and called in all five places. Its parameters, low and high, mean it can be reused for other ranges too. Inside, a while loop keeps asking until the input is valid: isdigit acts as a type check so letters cannot cause an error, and a range check rejects anything less than 1 or greater than 10. The function then returns the valid number. Because the code is in one place, a bug only has to be fixed once, and the whole program is shorter and easier to read.
This or That
defA Python function starts with def, then its name, then its parameters in brackets.- 12 4 is passed in as n, and 4 times 3 is 12.
- Sends a value back to the code that called it return hands an answer back. print only shows text and gives nothing back.
- Fixing it once instead of ten times With a function the code lives in one place, so one fix mends every use of it.
- Presence check A presence check makes sure something has been entered.
- Type check A type check makes sure the data is the right type, here a whole number.
- It keeps asking until the input is acceptable The loop repeats the question until the input passes every check, instead of crashing.
- False isdigit is True only when every character is a digit, and the a is not.
Memory Match
- A 4
def: Starts a function definition - B 6 Parameter: A value a function needs, named in its brackets
- C 1
return: Sends an answer back from a function - D 2 Presence check: Something has been entered
- E 3 Range check: The value lies between two limits
- F 5 Length check: The right number of characters