Popcorn Hack 1: Simple Procedure Call & Parameters

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("New York", "Active")
report_status("Los Angeles", "Resolved")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Code Example: Decomposition with Helper Procedures

Code Runner Challenge

Complex Decomposition Example

View IPYNB Source
# CODE_RUNNER: Complex Decomposition Example
# Complex Decomposition Example
# Helper Procedure
def check_priority(incident_type):
    return incident_type == "Medical Emergency"

# Outer Procedure
def process_incident(location, incident_type):
    priority = check_priority(incident_type)
    if priority:
        print("PRIORITY RESPONSE: " + incident_type + " reported in " + location)
    else:
        print("INCIDENT RECORDED: " + incident_type + " reported in " + location)

# Test calls
process_incident("Poway", "Medical Emergency")
process_incident("Poway", "Power Outage")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 2: Decomposition & Helper Procedures

Code Runner Challenge

Procedures Popcorn 2 - Decomposition & Helper Procedures

View IPYNB Source
# CODE_RUNNER: Procedures Popcorn 2 - Decomposition & Helper Procedures
# Helper Procedure
def is_priority(incident_type):
    # TODO: Return True when the incident is a "Medical Emergency"
    return incident_type == "Medical Emergency"


# Outer Procedure
def process_report(location, incident_type):
    # TODO: Call is_priority() using incident_type
    # TODO: Store the returned value in a variable
    priority = is_priority(incident_type)

    # TODO: Use an if/else statement
    # TODO: Print a priority message if priority is True
    # TODO: Otherwise, print a standard report message
    if priority:
        print("PRIORITY RESPONSE: " + incident_type + " reported in " + location)
    else:
        print("INCIDENT RECORDED: " + incident_type + " reported in " + location)


# TODO: Call process_report() two times
# TODO: Test it with different incident types
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

Code Runner Challenge

Procedures Homework - Dispatch Response

View IPYNB Source
# CODE_RUNNER: Procedures Homework - Dispatch Response
# Homework Hack
def check_priority(incident_type):
    # TODO: Return True when the incident requires priority handling
    return incident_type == "Medical Emergency" or incident_type == "Fire"

def dispatch_response(location, incident_type):
    # TODO: Call check_priority()
    # TODO: Store the returned value in a variable
    priority = check_priority(incident_type)
    
    # TODO: Use an if/else statement to print the appropriate response
    if priority:
        print("URGENT DISPATCH: High priority response for " + incident_type + " at " + location)
    else:
        print("STANDARD DISPATCH: Routine response for " + incident_type + " at " + location)

# TODO: Call dispatch_response() at least three times
dispatch_response("Poway", "Medical Emergency")
dispatch_response("San Diego", "Noise Complaint")
dispatch_response("San Diego", "Fire")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

MC Check

B. A named set of reusable code instructions

B. Defining creates the procedure, while calling executes it

B. A parameter

B. The actual value passed into a procedure when it is called

A. They allow the same procedure to work with different data