Stubs: placeholder code and comments for future development

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

Stubs: placeholder code and comments for future development

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which function is a valid stub in Python?
A def calculate_total(): pass
B def calculate_total():
2. What is the main purpose of a stub?
A Make the finished function run faster
B Mark work still to do so the rest of the program can run
3. Which comment is the usual way to flag a stub that needs to be finished?
A # TODO: add the discount calculation
B # This function is complete
4. What does the pass statement do when a stub is called?
A Stops the program with an error
B Nothing, and the program carries on
5. A stub for get_tax_rate() must give the rest of the program a number to test with. Which stub does this?
A def get_tax_rate(): return 0.1
B def get_tax_rate(): pass

Match the Pairs

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

Left columnRight column
1def save_scores(): pass AA comment that marks work still to do, so developers can find it later and replace the stub with real code.
2# TODO: add validation Bclass Player: pass # TODO: add attributes
3A stub can return a fixed test value so the rest of the program can be tested before the real calculation is written. CThe stub is replaced by real code but pass was left in. It is now unnecessary and misleading, so delete pass when the real code goes in.
4A stub for a class uses pass as its body, so objects can be created before its attributes and methods are written. DThe function has no body. A comment is not a statement, so Python raises an IndentationError. Add an indented pass line.
5def load_data(): # TODO: read the file Edef get_total(): return 100 # TODO: real total
6def show_menu(): pass print('Menu') FA stub function: it has a name but an empty body, so the program runs while the real code is still to be written.

Definitions

pass

Fill in the Blanks

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

1.
def total(): # TODO: add up prices ____
2.
___ save(): # TODO: write to file pass
3.
def getmark(): # ____: read the mark pass
4.
def price(item): # TODO: look up price ______ 9.99
5.
def double(n): # stub was: pass return n _ 2

Spot the Error

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

Question 1
A def show_menu():
B # TODO: print the menu
C pass
D show_menu(1)
Question 2
A def save_data():
B // TODO: write to file
C pass
D save_data()
Question 3
A def load_scores()
B # TODO: read the file
C pass
D load_scores()
Question 4
A def get_age():
B # TODO: use input()
C return 16
D age == get_age()
E print(age)
Question 5
A Class Player:
B # TODO: add attributes
C pass
D p = Player()

Answer Key

Stubs: placeholder code and comments for future development
Section A: Quick Questions
Q1
A — def calculate_total(): pass
Q2
B — Mark work still to do so the rest of the program can run
Q3
A — # TODO: add the discount calculation
Q4
B — Nothing, and the program carries on
Q5
A — def get_tax_rate(): return 0.1
Section B: Match the Pairs
Q1
def save_scores(): pass → A stub function: it has a name but an empty body, so the program runs while the real code is still to be written.
Q2
# TODO: add validation → A comment that marks work still to do, so developers can find it later and replace the stub with real code.
Q3
A stub can return a fixed test value so the rest of the program can be tested before the real calculation is written. → def get_total(): return 100 # TODO: real total
Q4
A stub for a class uses pass as its body, so objects can be created before its attributes and methods are written. → class Player: pass # TODO: add attributes
Q5
def load_data(): # TODO: read the file → The function has no body. A comment is not a statement, so Python raises an IndentationError. Add an indented pass line.
Q6
def show_menu(): pass print('Menu') → The stub is replaced by real code but pass was left in. It is now unnecessary and misleading, so delete pass when the real code goes in.
Section C: Definitions
Q1
pass — A Python statement that does nothing. It fills a stub's empty body, often beside a # TODO comment, so the program still runs while the real code is written later.
Section D: Fill in the Blanks
Q1
def total(): # TODO: add up prices pass
Q2
def save(): # TODO: write to file pass
Q3
def getmark(): # TODO: read the mark pass
Q4
def price(item): # TODO: look up price return 9.99
Q5
def double(n): # stub was: pass return n * 2
Section E: Spot the Error
Q1
D — The stub show_menu() has no parameters, so passing 1 causes a TypeError. The call should be show_menu() with empty brackets.
Q2
B — Python comments start with #, not //. The TODO comment should be '# TODO: write to file'.
Q3
A — The function header is missing its colon. It should be 'def load_scores():' so the indented stub body belongs to it.
Q4
D — == compares values and age does not exist yet, so this causes a NameError. Assignment needs a single =: age = get_age().
Q5
A — Python keywords are case-sensitive. The keyword must be lowercase: 'class Player:'.