Python lesson 3 of 50 · Beginner · About 20 minutes
A trip to the Workshop costs power. Give Pip a battery budget and work out how much energy the rover has left after travelling and recharging.

Mission Use integer values and arithmetic to calculate the rover’s remaining energy after a journey.
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.
Use a number for the battery
energy = 8 stores an integer, a whole number. It has no quotes. In a calculation, Python can subtract from the number 8. Quoted "8" is text, so Python will not treat it as an amount you can subtract from.
Calculate the whole cost
The star * means multiply. Python reads moves * cost_per_move as two moves times two units each. It stores the answer, 4, in the variable trip_cost.
Read the right-hand side first
In energy = energy - trip_cost, Python reads the old battery level on the right, subtracts the cost, then updates the name on the left. That makes it possible to use the word energy to mean Pip’s latest battery level through the whole program.
Add the recharge
energy = energy + recharge reads the current level after travel and adds the three units stored in recharge. The new battery level is seven. Here the plus sign combines two numbers.
Check each result as you go
The print calls report the values in order. The first report reads the starting level. The next reports let you check that multiplication, subtraction and addition happened in the order you meant.
How the pieces fit Inputs to a calculation can come from variables. Storing each result makes it available to the next calculation or report.
-
Start with 8 energy, 2 moves and a cost of 2 for each move.
Journey cost: 4 Why Two moves multiplied by two energy units gives the total travel cost. -
Subtract trip_cost from energy.
Energy after the journey: 4 Why Look up the old 8 first, calculate 8 − 4 and store the answer back in energy. -
Add recharge to the new energy.
Energy after recharge: 7 Why The battery now starts this calculation at 4, so 4 + 3 gives 7.
Predict before you run If moves changes from 2 to 3, what are the journey cost, remaining energy and final energy?
Reveal the explanation
3 * 2 gives a cost of 6. 8 - 6 leaves 2. 2 + 3 brings the final level to 5.
Build the checkpoint
Python skills Integers, Addition and subtraction, Multiplication, Reassignment.
1. Store numbers without quotes
Pip begins with eight energy units. Write energy = 8 with no quotation marks. Python calls a whole number like this an integer. The rover stores a number so you can do arithmetic. Keep the number of moves and the number of units each move costs in their own variables.
energy = 8
moves = 2
cost_per_move = 2
recharge = 3
2. Calculate the route cost
Use multiplication to work out the total cost. The star symbol * multiplies the number of moves by the energy needed for a single move. For two moves at two units each, you should get four. Saving that answer as trip_cost makes the next line easier to understand.
trip_cost = moves * cost_per_move
3. Update the reading after each event
When Python sees energy = energy - trip_cost, it works out the right-hand side using the old value first. Then it stores the result back under energy. The charging pad works the same way with addition. Read the two outputs as separate moments in Pip’s journey and compare them with your own calculation.
energy = energy - trip_cost
Complete Python checkpoint · moonbase_03.py
rover = "Pip"
energy = 8
moves = 2
cost_per_move = 2
recharge = 3
trip_cost = moves * cost_per_move
print(rover, "starts with", energy, "units.")
print("Journey cost:", trip_cost)
energy = energy - trip_cost
print("Energy after the journey:", energy)
energy = energy + recharge
print("Energy after recharge:", 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–15
-
Line 1
rover = "Pip" - Work out the value on the right, then store it in rover.
-
Line 2
energy = 8 - Work out the value on the right, then store it in energy.
-
Line 3
moves = 2 - Work out the value on the right, then store it in moves.
-
Line 4
cost_per_move = 2 - Work out the value on the right, then store it in cost_per_move.
-
Line 5
recharge = 3 - Work out the value on the right, then store it in recharge.
-
Line 7
trip_cost = moves * cost_per_move - Work out the value on the right, then store it in trip_cost.
-
Line 8
print(rover, "starts with", energy, "units.") - Show the message or value in the output.
-
Line 9
print("Journey cost:", trip_cost) - Show the message or value in the output.
-
Line 11
energy = energy - trip_cost - Work out the value on the right, then store it in energy.
-
Line 12
print("Energy after the journey:", energy) - Show the message or value in the output.
-
Line 14
energy = energy + recharge - Work out the value on the right, then store it in energy.
-
Line 15
print("Energy after recharge:", energy) - Show the message or value in the output.
Test the mission
- Before running, predict the numbers for two moves and a recharge worth three units.
- Change moves to 3 and check that six units are used. Restore it afterwards.
- Change recharge to 0. The battery reading should stay the same after the recharge step.
If something goes wrong
- Python will not subtract a value.
- Check that the battery values are numbers without quotation marks.
- My first energy report shows the lower reading.
- Place that print instruction before you subtract the journey cost.
Mission extension
Give Pip a return trip of one extra move. Print the energy after the return and explain why the order of recharge and travel matters to the report.