Space Python Loading Python…

Names that explain

Fix the codeEasy 3 stages

Somebody wrote a quick calculation for the cargo module and called the variables a, b, x2 and tmp. The program works, but nobody, including its author a month from now, can tell what it calculates without working through every line.

Predict what it prints, find out what each variable holds, then rename them so the code reads like a sentence. The output has to stay exactly the same.

Don't run the code yet. Read it and decide what it will print. 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.

Follow the values line by line: x2 first, then tmp, rounded to one decimal place.

Predict, then run
Leave the program as it is

Finish stage 1 to unlock this stage.

Investigate. The program checks how much room is left in a cargo module. Work out which variable holds the module's capacity in kg and which holds the free space as a percentage, and store their current names as text in capacity_name and percent_name.

Modify. Rename a, b, x2 and tmp to names that say what they hold, such as capacity_kg, loaded_kg, free_kg and free_percent. Change every place each name is used. The output must not change:

820 kg, 68.3%
Work out what each name holds
Rename, keep the output

Finish stage 2 to unlock this stage.

Write a fuel check with good names from the first line. The tank holds 8000 kg and the next burn uses 2350 kg. Store both in variables, work out what's left in kg and as a percentage of the tank, and print:

Fuel left: 5650 kg (70.6%)

No one- or two-letter names this time.

Print the fuel check
Use names that explain
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

Creating Variables

creating-variables-python.py
# Variables store data under a name
# Create them by assigning a value with =

ship_name = "Project Hail Mary"
crew_count = 3
fuel_remaining = 78.4
in_deep_space = True

print("Ship:", ship_name)
print("Crew:", crew_count, "people")
print("Fuel remaining:", fuel_remaining, "%")
print("In deep space:", in_deep_space)
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:].