Space Python Loading Python…

Units and separators

Write the codeEasy 3 stages

Sensors measure with more precision than anyone needs to read. The navigation computer reports the speed as 7.6612 and the mass as 1234567, and nobody on the flight deck wants to count digits during a launch.

Format the numbers for people: a sensible number of decimal places, thousands separators and the right units. The stored values stay exact; only the way they're printed changes.

Print the speed rounded to two decimal places, with its unit:

Speed: 7.66 km/s

Inside the curly braces, add a format spec after a colon: {speed:.2f} means "a number with 2 digits after the decimal point".

Print the speed
Format the value, not the text

Finish stage 1 to unlock this stage.

Add the mass, with commas between groups of three digits:

Speed: 7.66 km/s
Mass: 1,234,567 kg

The format spec , adds the separators: {mass:,}.

Add the mass
Format the values, not the text

Finish stage 2 to unlock this stage.

The fuel sensor reports how full the tank is as a fraction: 0.8734 means 87.34%. Add it as a percentage with one decimal place:

Speed: 7.66 km/s
Mass: 1,234,567 kg
Fuel: 87.3%

The format spec .1% multiplies by 100, rounds to 1 decimal place and adds the percent sign.

Add the fuel level
Format the values, not the text
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

Text Formatting with f-strings

text-formatting-f-strings.py
# F-strings - modern text formatting
astronaut = "John Smith"
mission = "Mars Explorer"
days_in_space = 180

print(f"Astronaut: {astronaut}")
print(f"Mission: {mission}")
print(f"Duration: {days_in_space} days")

# We can also do calculations
print(f"That is {days_in_space / 30:.1f} months")
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:].