Python 3.12: Calling Procedures HW
Python 3.12 Calling Procedures
The notebook includes the Quick Knowledge Check, both Popcorn Hacks, and the emergency-response Homework Hack using procedures, parameters, arguments, return values, and helper procedures.
Quick Knowledge Check
Answers: B, B, B, B, A
Score: 5/5
- B: A procedure is a named set of reusable code instructions.
- B: Defining creates the procedure; calling executes it.
- B:
locationis a parameter in the procedure definition. - B: An argument is the actual value passed when a procedure is called.
- A: Parameters let one procedure work with different data.
Popcorn Hack 1: Simple Procedure Call and Parameters
Define show_status(location, status) and call it twice with different arguments.
Code Runner Challenge
Procedures Popcorn #1
View IPYNB Source
# CODE_RUNNER: Procedures Popcorn #1
# Parameters make this procedure reusable with different locations and statuses.
def show_status(location, status):
print("Status update from " + location + ": " + status)
# These calls pass different arguments into the same procedure.
show_status("Poway", "Shelter open")
show_status("Rancho Bernardo", "Supplies delivered")
Popcorn Hack 2: Decomposition and Helper Procedures
The helper needs_priority() handles one small decision. The outer handle_report() calls that helper and uses its returned Boolean to choose the response.
Code Runner Challenge
Popcorn Hack 2 - Decomposition & Helper Procedures
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 2 - Decomposition & Helper Procedures
# The helper procedure returns a Boolean that the outer procedure can reuse.
def needs_priority(incident_type):
return incident_type == "Medical Emergency"
# The outer procedure delegates the priority check to the helper.
def handle_report(location, incident_type):
priority = needs_priority(incident_type)
if priority:
print("PRIORITY: " + incident_type + " reported in " + location)
else:
print("STANDARD REPORT: " + incident_type + " reported in " + location)
# Test both a priority and a standard incident.
handle_report("Poway", "Medical Emergency")
handle_report("Poway", "Power Outage")
Homework Hack: Emergency Response Dispatcher
This program uses a helper procedure named priority_incident() and an outer procedure named send_response(). The outer procedure calls the helper and prints a different response based on the Boolean it returns.
Code Runner Challenge
3.12 Homework - emergency response dispatcher
View IPYNB Source
# CODE_RUNNER: 3.12 Homework - emergency response dispatcher
# The helper identifies incidents that need immediate priority handling.
def priority_incident(incident_type):
return incident_type in ["Medical Emergency", "Fire"]
# The outer procedure calls the helper instead of repeating the priority logic.
def send_response(location, incident_type):
priority = priority_incident(incident_type)
if priority:
print("PRIORITY DISPATCH: Send responders to " + location + " for " + incident_type)
else:
print("STANDARD DISPATCH: Record " + incident_type + " in " + location)
# Three calls use different incident types and more than one location.
send_response("Poway", "Medical Emergency")
send_response("Rancho Bernardo", "Power Outage")
send_response("Poway", "Fire")
Explanation
Using a helper procedure keeps the priority rule in one place instead of repeating the same comparison inside every dispatch. Parameters make both procedures reusable because the same code can process different locations and incident types.