Methods with parameters and return values (Python)

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

This Methods with parameters and return values (Python) worksheet is part of the ACARA Digital Technologies Years 9–10 — Modular and Object-Oriented Programming (Python) journey. Print it as a booklet?

Methods with parameters and return values (Python)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which method header correctly takes an amount to deposit?
A def deposit(self, amount):
B def deposit(amount, self):
2. A BankAccount has self.balance = 100. What does account.deposit(50) do if deposit is def deposit(self, amount): self.balance += amount?
A balance becomes 150
B balance stays 100
3. When you call account.deposit(50), how many arguments do you write inside the brackets?
A Two: account and 50
B One: 50 (Python passes self for you)
4. Which method sends a calculated value back to the code that called it?
A def get_double(self): return self.balance * 2
B def get_double(self): print(self.balance * 2)
5. Given def get_total(self): return self.price * self.qty, which line stores the result?
A total = item.get_total
B total = item.get_total()

Match the Pairs

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

Left columnRight column
1def deposit(self, amount): AA method that takes an extra parameter, amount, after self so the caller can pass in a value to use
2return self.balance * 0.05 BThe method is missing self as its first parameter, so it cannot access self.balance and the call will fail with a TypeError.
3A method changes an attribute using the value passed in as a parameter CThe method uses print instead of return, so it only displays the value and the caller gets None back.
4A method checks a value with an if statement before it changes an attribute Dself.balance += amount
5def deposit(amount): self.balance += amount Eif amount > 0: self.balance += amount
6def get_interest(self): print(self.balance * 0.05) FSends a calculated value back to the code that called the method

Definitions

def deposit(self, amount):

Fill in the Blanks

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

1.
def deposit(self, ______): self.balance += amount
2.
def deposit(self, amount): self.balance __ amount
3.
def area(self): ______ self.width * self.height
4.
def withdraw(self, amount): if amount __ self.balance: self.balance -= amount
5.
acc = Account(100) acc._______(50) print(acc.balance)

Spot the Error

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

Question 1
A class Account:
B def deposit(self, amount):
C self.balance + amount
D return self.balance
Question 2
A class Account:
B def deposit(amount):
C self.balance += amount
Question 3
A class Wallet:
B def spend(self, amount)
C self.cash -= amount
D return self.cash
Question 4
A class Account:
B def withdraw(self, amount):
C if amount = self.bal:
D self.bal -= amount
Question 5
A class Item:
B def total(self, qty):
C sub = self.price * qty
D print(sub)

Answer Key

Methods with parameters and return values (Python)
Section A: Quick Questions
Q1
A — def deposit(self, amount):
Q2
A — balance becomes 150
Q3
B — One: 50 (Python passes self for you)
Q4
A — def get_double(self): return self.balance * 2
Q5
B — total = item.get_total()
Section B: Match the Pairs
Q1
def deposit(self, amount): → A method that takes an extra parameter, amount, after self so the caller can pass in a value to use
Q2
return self.balance * 0.05 → Sends a calculated value back to the code that called the method
Q3
A method changes an attribute using the value passed in as a parameter → self.balance += amount
Q4
A method checks a value with an if statement before it changes an attribute → if amount > 0: self.balance += amount
Q5
def deposit(amount): self.balance += amount → The method is missing self as its first parameter, so it cannot access self.balance and the call will fail with a TypeError.
Q6
def get_interest(self): print(self.balance * 0.05) → The method uses print instead of return, so it only displays the value and the caller gets None back.
Section C: Definitions
Q1
def deposit(self, amount): — A method can take extra parameters after self, like amount, and use return to send a value back to the code that called it.
Section D: Fill in the Blanks
Q1
def deposit(self, amount): self.balance += amount
Q2
def deposit(self, amount): self.balance += amount
Q3
def area(self): return self.width * self.height
Q4
def withdraw(self, amount): if amount <= self.balance: self.balance -= amount
Q5
acc = Account(100) acc.deposit(50) print(acc.balance)
Section E: Spot the Error
Q1
C — This line only calculates a value and throws it away, so the attribute never changes. Use += (self.balance += amount) to update the balance.
Q2
B — Every method must list self as its first parameter before any extra parameters. It should be def deposit(self, amount):
Q3
B — A method definition must end with a colon. It should be def spend(self, amount):
Q4
C — A single = assigns a value, but a condition needs a comparison. To check the amount is not more than the balance, use if amount <= self.bal:
Q5
D — print() only shows the value on screen and the method returns None. To send the value back to the caller, use return sub.