Python 3.13: Developing Procedures HW
Python 3.13 Developing Procedures
The current lesson does not define a separate Popcorn or Homework section, so this notebook completes the procedure mini-game idea and all three exercises listed in the lesson.
Practice 1: Rename Procedures Clearly
Descriptive procedure names make the call sequence readable without opening every function.
Code Runner Challenge
3.13 Practice 1 - descriptive procedure names
View IPYNB Source
# CODE_RUNNER: 3.13 Practice 1 - descriptive procedure names
# Each helper name describes exactly one dessert-making responsibility.
def measure_dry_ingredient():
print("Measuring 2 cups of flour.")
def mix_batter():
print("Mixing flour, sugar, and eggs.")
def bake_batch():
print("Baking at 350°F for 25 minutes.")
# The main procedure reads like a short plan because the helper names explain the steps.
def make_batch():
measure_dry_ingredient()
mix_batter()
bake_batch()
make_batch()
Practice 2: Equivalent Java Methods
Code Runner Challenge
3.13 Practice 3 - decompose a long procedure
View IPYNB Source
public static void makeDessert() {
measureFlour();
mixDessertIngredients();
bakeDessert();
}
private static void measureFlour() {
System.out.println("Measuring 2 cups of flour.");
}
private static void mixDessertIngredients() {
System.out.println("Mixing flour, sugar, and eggs.");
}
private static void bakeDessert() {
System.out.println("Baking at 350°F for 25 minutes.");
}
public makes makeDessert() callable from outside its class. The helper methods can be private because only the class itself needs to call them.
Practice 3: Split a Long Procedure
Breaking one large procedure into small helpers makes each step easier to test independently.
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
# Each helper returns a value so the step can be tested without running the entire program.
def pick_ingredient():
return "flour"
def mix(item):
return "Mixing " + item + " into the bowl."
def heat():
return "Heating the oven to 350°F."
def finish_recipe():
return "Dessert is ready!"
# The outer procedure combines the small, testable pieces into the full workflow.
def make_batch():
ingredient = pick_ingredient()
return [mix(ingredient), heat(), finish_recipe()]
for step in make_batch():
print(step)
# Direct tests make it obvious which helper fails if behavior changes.
print("mix test:", mix("sugar") == "Mixing sugar into the bowl.")
print("heat test:", heat() == "Heating the oven to 350°F.")
Mini-Game Practice: Procedure Naming
The lesson’s original mini-game uses input(). This runner-friendly version uses a stored name so it can run on the portfolio without waiting for keyboard input.
# CODE_RUNNER: 3.13 Mini-game - show why clear procedure names matter
import random
# These helpers model the same dessert sequence from the lesson mini-game.
def pick_ingredient():
return random.choice(["flour", "sugar", "eggs", "milk"])
def run_recipe_step():
ingredient = pick_ingredient()
print("Mixing", ingredient, "into the bowl.")
print("Heating the oven to a friendly temperature...")
print("Dessert is ready!")
# A descriptive call explains the goal immediately; a vague name like do_step() would not.
print("Readable procedure call: run_recipe_step()")
run_recipe_step()