Python lesson 9 of 50 · Beginner · About 30 minutes
Every route spends energy, but the cost varies. Write one reusable travel rule and test how it behaves with two different journeys.

Mission Define a function with two parameters and return the resulting battery value.
Get ready
Read this as the next checkpoint in Pip’s mission. You can also start with the complete program below.
Open the browser Python editor (opens in a new tab), replace its example with the complete checkpoint below and choose Run. Use Python 3.10 or newer.

Understand the Python in this lesson
Read the explanations, then follow the worked example before changing the code.
Define a reusable instruction
def travel(energy, cost): defines a function called travel. The colon starts the indented function body. Python learns the recipe at this line. The body runs when another line calls the function.
Give names to the two inputs
energy and cost in the header are parameters, names for the information the function needs. In travel(energy, workshop_cost), the supplied values are arguments. Their order matters. The first feeds the energy parameter and the second feeds the cost parameter.
Return the right value for each case
The less-than sign in energy < cost asks whether the battery has too little energy. If it does, return energy hands the old value back and ends that call. Otherwise return energy - cost hands back what remains.
Use a returned answer
In energy = travel(energy, workshop_cost), Python calculates the function’s answer first and then updates the mission’s variable. The similarly named energy parameter belongs to that individual call. It is the assignment outside the function that saves the new mission value.
Test the exact boundary
energy >= workshop_cost means enough energy includes an exact match. Six units can pay a cost of six and leave zero. Six units cannot pay a cost of seven, so that trip call returns the original six.
How the pieces fit The caller decides when to attempt a journey and what to report. The travel function keeps the battery rule in one place that both journeys can use.
-
Set energy to 8 and the Workshop cost to 2.
The Workshop condition is true. Why Eight is greater than or equal to two, so the call inside the branch runs. -
Call travel with 8 and 2.
Workshop reached. Energy: 6. Why Inside travel, 8 is not less than 2, so it returns 6 and the caller stores it. -
Check the Beacon cost of 7 with the updated energy.
Beacon trip cancelled. Recharge first. Energy after the Beacon check: 6. Why The caller announces the problem and the function returns the original energy unchanged.
Predict before you run If you call travel(8, 2) by itself on a line, where does the returned 6 go?
Reveal the explanation
The function still calculates 6, but that call does nothing with the answer. Assign the call to a variable to keep it, or give the call to print to show the result.
Build the checkpoint
Python skills def, Parameters and arguments, return, Testing a boundary.
1. Name a small job
Start with a function called travel. Its two parameters are energy and cost. A parameter is a name the function uses for a value given later. The indented instructions form the function’s body. You can read the body as a rule that works for many different battery readings.
def travel(energy, cost):
if energy < cost:
return energy
return energy - cost
2. Send two values to the rule
On launch, the rover has eight units of energy and the Workshop costs two. Call travel(energy, workshop_cost) to pass these values into the function. The function returns six. Assign that result back to energy; otherwise your outer battery variable would still hold eight.
if energy >= workshop_cost:
energy = travel(energy, workshop_cost)
print(f"Workshop reached. Energy: {energy}.")
3. Test the limit and the blocked case
The Beacon’s original route costs seven, so the rover with six units leaves its power reading alone. Change the Beacon cost to six and run again. The condition uses < to check that energy is strictly less than the cost. Being exactly equal still allows the trip and leaves zero units.
if energy < beacon_cost:
print("Beacon trip cancelled. Recharge first.")
energy = travel(energy, beacon_cost)
Complete Python checkpoint · moonbase_09.py
def travel(energy, cost):
if energy < cost:
return energy
return energy - cost
energy = 8
workshop_cost = 2
if energy >= workshop_cost:
energy = travel(energy, workshop_cost)
print(f"Workshop reached. Energy: {energy}.")
beacon_cost = 7
if energy < beacon_cost:
print("Beacon trip cancelled. Recharge first.")
energy = travel(energy, beacon_cost)
print(f"Energy after the Beacon check: {energy}.")Read the complete checkpoint line by line
Use this guide beside the checkpoint. Blank lines separate ideas. Indented lines belong to the instruction above them. Lines with brackets still open can carry on below.
Lines 1–16
-
Line 1
def travel(energy, cost): - Define travel, a reusable piece of the program. Its input names are energy, cost. The indented body runs when something calls it.
-
Line 2
if energy < cost: - Run the indented block only if this check is true.
-
Line 3
return energy - Leave this function now and hand the result back to the line that called it.
-
Line 4
return energy - cost - Leave this function now and hand the result back to the line that called it.
-
Line 7
energy = 8 - Work out the value on the right, then store it in energy.
-
Line 8
workshop_cost = 2 - Work out the value on the right, then store it in workshop_cost.
-
Line 9
if energy >= workshop_cost: - Run the indented block only if this check is true.
-
Line 10
energy = travel(energy, workshop_cost) - Work out the value on the right, then store it in energy. Run travel with the supplied inputs and come back here when it returns.
-
Line 11
print(f"Workshop reached. Energy: {energy}.") - Show the message or value in the output. Python fills the braces with values before it uses the text.
-
Line 13
beacon_cost = 7 - Work out the value on the right, then store it in beacon_cost.
-
Line 14
if energy < beacon_cost: - Run the indented block only if this check is true.
-
Line 15
print("Beacon trip cancelled. Recharge first.") - Show the message or value in the output.
-
Line 16
energy = travel(energy, beacon_cost) - Work out the value on the right, then store it in energy. Run travel with the supplied inputs and come back here when it returns.
Lines 17–17
-
Line 17
print(f"Energy after the Beacon check: {energy}.") - Show the message or value in the output. Python fills the braces with values before it uses the text.
Test the mission
- Compute both battery readings by hand before you press Run.
- Change Beacon cost to 6 and check the final energy is 0.
- List one place later in the game where the travel function could be reused.
If something goes wrong
- A call gives a value but energy stays the same.
- Assign the returned value to the battery variable with energy = travel(...).
- My function only handles one route.
- Use the cost parameter inside the function instead of a station-specific variable.
Mission extension
Write and test a second function called charge. Give it an energy value and a number of units to add, then use the returned value in the Beacon route.