Legacy script
Every mission has one: the script written the night before launch that everybody still runs and nobody dares to touch. This one prints the daily engine report. It repeats the same twelve lines for each engine, the variables are called t, m and s, and a fourth engine is being installed next month.
Restructure it without changing a single character of what it prints, then make sure it stays that way.
Finish stage 1 to unlock this stage.
Restructure the script around two functions, keeping the output exactly as it is now:
parse_feed(feed)returns a dict from each sensor to its list of values, as floats, in order;summary_line(sensor, values)returns one report line: the average rounded to one decimal place, the highest value, andHOTwhen it is over 450, otherwiseOK.
Then print the report with a loop over a list of the engines. The output, before and after:
ENGINE REPORT ============= ENG-1 avg 425.6 max 431.75 OK ENG-2 avg 400.4 max 402.25 OK ENG-3 avg 440.1 max 455.0 HOT Engines running hot: 1
Run the original first and keep its output to compare. The hidden tests also check that the copies of the block are gone.
Finish stage 1 to unlock this stage.
Add assert tests for both functions, at least four, between the functions and the report. They print nothing when all is well, so the output stays exactly the same. Test parse_feed() with a small feed of your own, and summary_line() with an engine that is HOT and one that is OK.
The hidden tests break each of your functions in a quiet way, one dropping the last reading of every engine and the other ignoring the first value and never saying HOT, and run your program again: your asserts must stop both.
Hints
Creating Simple Functions
# 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
Separate arguments with commas. They're passed on every Run as sys.argv[1:].