Building a modular program with functions, lists and classes

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

Building a modular program with functions, lists and classes

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which plan shows how a big problem is broken into smaller tasks before you code?
A A hierarchy chart
B A list of variable names
2. In a modular program, where should the data about each thing (like a Student) live?
A In one long main() function with many loose variables
B In a class, with one object per thing
3. Which function header handles a list of objects the best?
A def show_all(students):
B def show_all(): # uses a global list
4. What should main() mostly do in a modular program?
A Run the menu loop and call small functions
B Hold all the code for every task
5. Which line adds a new object to a list called pets?
A pets.Pet('Rex', 3)
B pets.append(Pet('Rex', 3))

Match the Pairs

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

Left columnRight column
1pets = [] ACreates an empty list that will hold the program's objects, such as Pet objects
2def show_all(pets): Bmain is written without brackets, so the function is never called and the program does nothing. It should be main().
3main() runs a menu loop that keeps asking the user what to do until they choose to quit Cpets.append(Pet("Rex", 3))
4A class describes one kind of thing, and each object made from it is stored in the list DA small function that receives the list of objects so it can handle them, for example printing each one
5def add_pet(name, age): pets.append(Pet(name, age)) return pets.append Ewhile choice != "q":
6main print("Program finished") FThe function ends with a broken return that does not give back anything useful. Each small function should do one clear job, so this one should just add the pet and need no return.

Definitions

modular program

Fill in the Blanks

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

1.
_____ Song: def __init__(self, title): self.title = title
2.
songs = [] songs.______(Song("Hello"))
3.
def show(songs): for song in _____: print(song)
4.
def main(): _____ True: choice = input("Choice: ")
5.
songs = [] songs.append(Song("Hello")) assert ___(songs) == 1

Spot the Error

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

Question 1
A class Pet
B def __init__(self, name):
C self.name = name
D pets = []
Question 2
A def add_pet(pets, name):
B pet = Pet(name)
C pets.add(pet)
D return pets
Question 3
A def show_all(pets):
B for pet of pets:
C print(pet.name)
Question 4
A def main():
B pets = []
C if choice = 'y':
D if choice == 'y':
E add_pet(pets, 'Rex')
Question 5
A def count_big(pets, size):
B total = 0
C for pet in pets:
D if pet > size:
E total += 1
F return total

Answer Key

Building a modular program with functions, lists and classes
Section A: Quick Questions
Q1
A — A hierarchy chart
Q2
B — In a class, with one object per thing
Q3
A — def show_all(students):
Q4
A — Run the menu loop and call small functions
Q5
B — pets.append(Pet('Rex', 3))
Section B: Match the Pairs
Q1
pets = [] → Creates an empty list that will hold the program's objects, such as Pet objects
Q2
def show_all(pets): → A small function that receives the list of objects so it can handle them, for example printing each one
Q3
main() runs a menu loop that keeps asking the user what to do until they choose to quit → while choice != "q":
Q4
A class describes one kind of thing, and each object made from it is stored in the list → pets.append(Pet("Rex", 3))
Q5
def add_pet(name, age): pets.append(Pet(name, age)) return pets.append → The function ends with a broken return that does not give back anything useful. Each small function should do one clear job, so this one should just add the pet and need no return.
Q6
main print("Program finished") → main is written without brackets, so the function is never called and the program does nothing. It should be main().
Section C: Definitions
Q1
modular program — A program split into parts: a class for its things, a list of objects, and small functions called from main(), planned with diagrams and checked with tests.
Section D: Fill in the Blanks
Q1
class Song: def __init__(self, title): self.title = title
Q2
songs = [] songs.append(Song("Hello"))
Q3
def show(songs): for song in songs: print(song)
Q4
def main(): while True: choice = input("Choice: ")
Q5
songs = [] songs.append(Song("Hello")) assert len(songs) == 1
Section E: Spot the Error
Q1
A — A class header must end with a colon. It should be class Pet:
Q2
C — Lists have no add() method. Use pets.append(pet) to put the new object on the end of the list.
Q3
B — Python for loops use the keyword in, not of. It should be for pet in pets:
Q4
C — A single = assigns a value. To compare in an if statement use ==. It should be if choice == 'y':
Q5
D — pet is a whole object, so it can't be compared with a number. Compare the attribute: pet.size > size.