3.12 Calling Procedures HW
Popcorn Hack 1: Simple Procedure Call & Parameters
report_status has two parameters, location and status. It is called twice with different arguments, so the same code prints two different updates.
Code Runner Challenge
Procedures Popcorn #1
View IPYNB Source
# CODE_RUNNER: Procedures Popcorn #1
def report_status(location, status):
print("Status update from " + location + ": " + status)
report_status("Poway Community Park", "All clear")
report_status("Old Poway Park", "Downed tree blocking the trail")
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Popcorn Hack 2: Decomposition & Helper Procedures
The helper is_priority returns True only for a "Medical Emergency". The outer procedure process_report calls the helper, stores the answer, and prints a different message for each result.
Code Runner Challenge
Popcorn Hack 2 - Decomposition & Helper Procedures
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 2 - Decomposition & Helper Procedures
# Helper Procedure
def is_priority(incident_type):
return incident_type == "Medical Emergency"
# Outer Procedure
def process_report(location, incident_type):
priority = is_priority(incident_type)
if priority:
print("PRIORITY: " + incident_type + " reported in " + location)
else:
print("STANDARD REPORT: " + incident_type + " reported in " + location)
process_report("Poway", "Medical Emergency")
process_report("Poway", "Power Outage")
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Homework Hack: Emergency Dispatch Program
check_priority(incident_type)is the helper. It returns a Boolean:Truefor incidents that put lives at immediate risk (medical emergencies, fires, and gas leaks).dispatch_response(location, incident_type)is the outer procedure. It callscheck_priority, stores the result, and prints a priority dispatch or a scheduled response.dispatch_responseis called four times with different locations and incident types, so both paths run.
Code Runner Challenge
Homework Hack - Emergency Dispatch Program
View IPYNB Source
# CODE_RUNNER: Homework Hack - Emergency Dispatch Program
# Helper Procedure: returns True when the incident needs priority handling
def check_priority(incident_type):
priority_incidents = ["Medical Emergency", "Brush Fire", "Gas Leak"]
return incident_type in priority_incidents
# Outer Procedure: calls the helper, then picks the response
def dispatch_response(location, incident_type):
priority = check_priority(incident_type)
if priority:
print("DISPATCH NOW: Sending the response team to " + location + " for a " + incident_type + ".")
else:
print("SCHEDULED: " + incident_type + " at " + location + " added to the next volunteer shift.")
dispatch_response("Poway Road", "Medical Emergency")
dispatch_response("Lake Poway", "Brush Fire")
dispatch_response("Community Center", "Power Outage")
dispatch_response("Twin Peaks Road", "Fallen Tree")
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...