Space Python Loading Python…

Shadowed fuel

Fix the codeMedium 3 stages

The tanker docked, the pumps ran, the log says 300 kg were transferred. The fuel display still shows exactly what it showed before docking, and the flight director wants to know where 300 kg of propellant went.

Predict what the program prints, find the second fuel_kg, fix the function, then write one that can't fall into the same trap.

Don't run the code yet. Read it and decide what it will print on its last line. Write your guess in prediction, at the top, between the quotes. Then run it and compare.

Any guess passes this stage: the point is to think before you run. After you check, you'll see your prediction next to what Python printed.

How many fuel_kg variables are there while refuel() runs?

Predict, then run
Leave the program as it is

Finish stage 1 to unlock this stage.

Investigate. Print fuel_kg as the last line inside refuel() and store what it prints in inside_value. Store the global fuel_kg after the call in outside_value. Remove any lines you added before you check.

Modify. Pass the fuel into refuel(), return the new amount, and store it in fuel_kg at the call. It must print:

Refuelled with 300.0 kg
Fuel on board: 1500.0 kg
Find both variables
Return the new total

Finish stage 2 to unlock this stage.

Write fuel_report(fuel_kg, capacity_kg), which uses only its parameters, never the globals, and returns the line. Print it for the tank, then for 250.0 kg:

Fuel: 1500.0 of 2000.0 kg (75%)
Fuel: 250.0 of 2000.0 kg (12%)

The hidden test checks that the function reads no global names, so use capacity_kg, not CAPACITY_KG, inside it.

Two reports
No globals inside
You're not logged in, so your progress is kept in this browser only. Log in to save it, and to use hints and solutions.

Hints

Local and Global Variables

local-global-variables.py
# Created out here, so every function in the file can read them
mission_name = "Dragonfly"
fuel_kg = 400

def status():
    checked_at = "T minus 10"
    print(mission_name, "checked at", checked_at, "with", fuel_kg, "kg")

status()

# checked_at only existed inside status, so this cannot work
# print(checked_at)

def burn():
    fuel_kg = 250
    print("Inside burn, fuel is", fuel_kg)

burn()
print("Outside, fuel is still", fuel_kg)

# Two functions, the same local name, completely separate variables
def rotor_a():
    samples = 3
    print("Rotor A collected", samples)

def rotor_b():
    samples = 17
    print("Rotor B collected", samples)

rotor_a()
rotor_b()
Open the full example
script.py
PythonLn 1, Col 1Spaces: 4
Loading the Python environment…

Separate arguments with commas. They're passed on every Run as sys.argv[1:].