Passing a list into a function

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

Passing a list into a function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which function header takes a list called scores as a parameter?
A def total_of[scores]:
B def total_of(scores):
2. Which call correctly passes a list into total_of()?
A total_of([5, 8, 9])
B total_of(5, 8, 9)
3. Inside def total_of(scores):, what does scores hold when we call total_of([4, 6])?
A Only the number 4
B The list [4, 6]
4. Which loop lets a function read every item in its list parameter scores?
A for score in scores:
B for score in total_of:
5. Which statement about calling total_of() is true?
A You can call it with different lists, such as [5, 8] and [10, 20, 30]
B It only works with the first list it was ever called with

Match the Pairs

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

Left columnRight column
1def total_of(scores): ADefines a function with a parameter called scores that will receive a list
2total_of([5, 8, 9]) BCalls the function and passes in a list of three numbers
3The function can loop over every item in the list it was given CThe call uses a list variable that has not been created, so Python raises a NameError.
4The same function can be reused with different lists Dfor s in scores: total += s
5def total_of([5, 8, 9]): EA list value was put in the def line. The header needs a parameter name like scores, and the list is passed when calling the function.
6total_of(scores) # scores was never created Ftotal_of(maths) total_of(science)

Definitions

def total_of(scores):

Fill in the Blanks

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

1.
def total(______): result = 0 for s in scores:
2.
def total(scores): return ___(scores)
3.
def total(scores): result = 0 for score __ scores:
4.
marks = [70, 85, 90] print(total(_____))
5.
def average(scores): return sum(scores) / ___(scores)

Spot the Error

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

Question 1
A def total_of(scores)
B total = 0
C for s in scores:
D total += s
E return total
Question 2
A def average(marks):
B total = sum(marks)
C count = len(marks)
D return total / marks
E print(average([70, 80, 90]))
Question 3
A def biggest(nums):
B top = nums[0]
C for n in nums:
D if n > top
E top = n
F return top
Question 4
A def show_all(names):
B for name in names:
C print(nam)
D show_all(['Ava', 'Liam'])
Question 5
A def count_pass(marks):
B passed = 0
C for m in range(marks):
D if m >= 50:
E passed += 1
F return passed

Answer Key

Passing a list into a function
Section A: Quick Questions
Q1
B — def total_of(scores):
Q2
A — total_of([5, 8, 9])
Q3
B — The list [4, 6]
Q4
A — for score in scores:
Q5
A — You can call it with different lists, such as [5, 8] and [10, 20, 30]
Section B: Match the Pairs
Q1
def total_of(scores): → Defines a function with a parameter called scores that will receive a list
Q2
total_of([5, 8, 9]) → Calls the function and passes in a list of three numbers
Q3
The function can loop over every item in the list it was given → for s in scores: total += s
Q4
The same function can be reused with different lists → total_of(maths) total_of(science)
Q5
def total_of([5, 8, 9]): → A list value was put in the def line. The header needs a parameter name like scores, and the list is passed when calling the function.
Q6
total_of(scores) # scores was never created → The call uses a list variable that has not been created, so Python raises a NameError.
Section C: Definitions
Q1
def total_of(scores): — A function header with a list parameter. Pass a list when you call it, like total_of([5, 8, 9]), and the function can loop over or read its items.
Section D: Fill in the Blanks
Q1
def total(scores): result = 0 for s in scores:
Q2
def total(scores): return sum(scores)
Q3
def total(scores): result = 0 for score in scores:
Q4
marks = [70, 85, 90] print(total(marks))
Q5
def average(scores): return sum(scores) / len(scores)
Section E: Spot the Error
Q1
A — A function header must end with a colon. It should be def total_of(scores): so that the indented body belongs to the function.
Q2
D — You can't divide a number by a whole list, which causes a TypeError. Divide by the count of items, so it should be return total / count.
Q3
D — An if statement needs a colon at the end of the line. It should be if n > top: so the indented line below runs when the test is true.
Q4
C — The loop variable is called name, not nam. Using nam causes a NameError. It should be print(name).
Q5
C — range() needs a number, not a list, so this causes a TypeError. To loop over the items in the list parameter, write for m in marks:.