Python 3.12 - Calling Procedures HW
Python 3.12 Calling Procedures
Completed the Quick Knowledge Check, both Popcorn Hacks, and the IT help-desk 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 runs 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 report_status(location, status) and call it twice with different arguments.
Code Runner Challenge
Procedures Popcorn #1
View IPYNB Source
# CODE_RUNNER: Procedures Popcorn #1
# location and status are parameters, so one procedure can report on any room.
def report_status(location, status):
print(location + ": " + status)
report_status("Room 101", "Printer fixed")
report_status("Library", "Wi-Fi is back online")
Popcorn Hack 2: Decomposition and Helper Procedures
The helper is_priority() makes one small decision. The outer process_report() calls it and uses the 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 returns True or False.
def is_priority(incident_type):
return incident_type == "Server Down"
# The outer procedure calls the helper to decide what to print.
def process_report(location, incident_type):
if is_priority(incident_type):
print("PRIORITY: " + incident_type + " in " + location)
else:
print("STANDARD REPORT: " + incident_type + " in " + location)
process_report("Main Office", "Server Down")
process_report("Room 101", "Broken Mouse")
Homework Hack: IT Help-Desk Dispatcher
A helper named check_priority() and an outer procedure named dispatch_response() work together. The outer procedure calls the helper and prints a different response based on the Boolean it returns.
Code Runner Challenge
3.12 Homework - IT help-desk dispatcher
View IPYNB Source
# CODE_RUNNER: 3.12 Homework - IT help-desk dispatcher
# The helper decides which problems are urgent.
def check_priority(incident_type):
return incident_type in ["Server Down", "Network Outage"]
# The dispatcher calls the helper instead of repeating the urgent-problem rule.
def dispatch_response(location, incident_type):
if check_priority(incident_type):
print("URGENT: Send a technician to " + location + " for " + incident_type)
else:
print("ROUTINE: Add " + incident_type + " in " + location + " to the queue")
dispatch_response("Main Office", "Network Outage")
dispatch_response("Room 101", "Slow Computer")
dispatch_response("Library", "Server Down")
Explanation
Using a helper keeps the urgent-problem rule in one place instead of copying it into every dispatch. Parameters make both procedures reusable, because the same code can handle any location and any kind of problem.