Python 3.13 Developing Procedures

The lesson doesn’t have a separate Popcorn or Homework section, so this notebook completes the procedure mini-game idea and all three practice exercises, using a “get ready for school” morning routine.

Practice 1: Rename Procedures Clearly

Clear names tell you what each procedure does without reading its code.

Code Runner Challenge

3.13 Practice 1 - descriptive procedure names

View IPYNB Source
# CODE_RUNNER: 3.13 Practice 1 - descriptive procedure names
def pack_backpack():
    print("Packing laptop, binder, and water bottle.")

def eat_breakfast():
    print("Eating cereal and a banana.")

def walk_to_bus_stop():
    print("Walking to the bus stop at 7:15.")

# Reads like a checklist because the names explain each step.
def get_ready_for_school():
    pack_backpack()
    eat_breakfast()
    walk_to_bus_stop()

get_ready_for_school()
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Practice 2: Equivalent Java Methods

Code Runner Challenge

3.13 Practice 3 - decompose a long procedure

View IPYNB Source
public static void getReadyForSchool() {
    packBackpack();
    eatBreakfast();
    walkToBusStop();
}

private static void packBackpack() {
    System.out.println("Packing laptop, binder, and water bottle.");
}

private static void eatBreakfast() {
    System.out.println("Eating cereal and a banana.");
}

private static void walkToBusStop() {
    System.out.println("Walking to the bus stop at 7:15.");
}
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

public lets code outside the class call getReadyForSchool(). The helpers are private because only this class needs them.

Practice 3: Split a Long Procedure

Smaller procedures that return values can each be tested by themselves.

Code Runner Challenge

3.13 Mini-game - show why clear procedure names matter

View IPYNB Source
# CODE_RUNNER: 3.13 Practice 3 - decompose a long procedure
def pick_outfit():
    return "hoodie and jeans"

def get_dressed(outfit):
    return "Putting on " + outfit + "."

def grab_lunch():
    return "Grabbing lunch from the fridge."

def head_out():
    return "Out the door!"

def morning_routine():
    outfit = pick_outfit()
    return [get_dressed(outfit), grab_lunch(), head_out()]

for step in morning_routine():
    print(step)

# Test the helpers one at a time.
print("get_dressed test:", get_dressed("a jacket") == "Putting on a jacket.")
print("grab_lunch test:", grab_lunch() == "Grabbing lunch from the fridge.")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Mini-Game Practice: Procedure Naming

The lesson’s mini-game uses input(). This version picks a random choice instead, so it runs on the portfolio page without waiting for typing.

# CODE_RUNNER: 3.13 Mini-game - show why clear procedure names matter
import random

def choose_breakfast():
    return random.choice(["toast", "cereal", "eggs", "a smoothie"])

def start_the_day():
    food = choose_breakfast()
    print("Eating", food + ".")
    print("Brushing teeth...")
    print("Ready for school!")

# start_the_day() says what it does; a name like do_stuff() would not.
print("Readable procedure call: start_the_day()")
start_the_day()