Space Python Loading Python…

Log formatter function

Fix the codeEasy 2 stages

In "Log line format" you printed one mission log entry with an f-string. Since then the format has been copied into every program that writes to the log, and each copy has drifted a little.

Put the format in one function that returns the line, then see what happens when the format has to change.

Write a function format_log(t, level, msg) that returns the log line, and print what it returns. The output must stay exactly the same:

[T+00:12:05] INFO  Engines nominal

Define the function at the top: def format_log(t, level, msg):, with return f"..." inside, using its own parameter names. Then print(format_log(elapsed, level, message)).

Keep the output
A function that returns

Finish stage 1 to unlock this stage.

The function is now used for a whole orbit's entries, and ERROR is five letters long, so its messages start one column later than the others. Change only the function so the level is padded to five characters, followed by one space:

[T+00:12:05] INFO  Engines nominal
[T+00:14:47] ERROR Fuel valve 2 not responding
[T+00:15:42] WARN  Star tracker lost lock

{level:<5} pads the level with spaces to five characters, as in "Aligned status board".

Align the levels
The function does the padding
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 Simple Functions

creating-simple-functions.py
# Functions in Python

# A function is a reusable block of code
def mission_greeting():
    print("Welcome aboard the ISS")
    print("All systems nominal")

mission_greeting()

# Functions with a parameter
def check_oxygen(level):
    print(f"Oxygen level: {level}%")
    if level < 20:
        print("ALERT: Oxygen critically low")

check_oxygen(95)
check_oxygen(12)

# Functions that return a value
def earth_weight_to_moon(earth_kg):
    moon_gravity = 0.166
    return earth_kg * moon_gravity

astronaut_weight = earth_weight_to_moon(82)
print(f"Weight on Moon: {astronaut_weight:.1f} kg")

backpack_weight = earth_weight_to_moon(45)
print(f"Backpack on Moon: {backpack_weight:.1f} kg")

# Functions with multiple parameters
def travel_time(distance_km, speed_kph):
    hours = distance_km / speed_kph
    return hours

# Earth to Moon
moon_hours = travel_time(384400, 40000)
print(f"\nMoon trip: {moon_hours:.1f} hours")

# Earth to Mars
mars_hours = travel_time(225_000_000, 58000)
mars_days = mars_hours / 24
print(f"Mars trip: {mars_days:.0f} days")

# Try it yourself
# What does earth_weight_to_moon(0) return?
# Calculate travel time to Jupiter: 778_500_000 km
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:].