Changed the caller's list
The dashboard previews what the day's chart would look like with the next reading added. The preview works. But after every preview, the stored readings for the day have one more value in them, and the daily report counts readings that were never really taken.
Predict what the program prints, find out how the preview reached the stored list, fix the function, then write another one that leaves its input alone.
Finish stage 1 to unlock this stage.
Don't run the code yet. Read it and decide what it will print on its first 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.
Which list does append() add to: a new one, or today itself?
Finish stage 1 to unlock this stage.
Investigate. Check with with_new is today whether they are the same list, and store True or False in same_list. Store how many readings today holds after the call in today_count. Remove any lines you added before you check.
Modify. Make add_reading() return a new list and leave the one it receives alone. It must print:
Today: [412.5, 415.0] With the new reading: [412.5, 415.0, 418.2]
Finish stage 2 to unlock this stage.
Write without_spikes(readings, limit), which returns a new list of the readings at or below the limit and leaves readings unchanged. Then print:
Clean: [412.5, 415.0, 418.2] Stored: [412.5, 9999.0, 415.0, 418.2, 9999.0]
Build a new list in the function, as in "Filter the log", and return it.
Hints
Functions with Parameters
# Two parameters, separated by a comma
def solar_power(watts_at_earth, distance_au):
return watts_at_earth / (distance_au ** 2)
# The values you hand over at the call are called arguments
print("At Mars:", round(solar_power(1361, 1.52), 1), "W per square metre")
print("At Jupiter:", round(solar_power(1361, 5.2), 1), "W per square metre")
# Order matters. Swap the two and the answer is nonsense
print("Swapped:", solar_power(1.52, 1361))
# Arguments can be variables, not just numbers you typed in
solar_constant = 1361
saturn_au = 9.58
print("At Saturn:", round(solar_power(solar_constant, saturn_au), 1))
# Parameters can be any mix of types
def mission_tag(name, year, crewed):
return f"{name} launched in {year}, crewed: {crewed}"
print(mission_tag("BepiColombo", 2018, False))
# A parameter is a local name. Changing it never touches the original
def rename(label):
label = "changed inside"
return label
original = "untouched outside"
print(rename(original), "|", original)
# Too few arguments, or too many, stops the program
# print(solar_power(1361)) # delete the hash sign to meet a type error
Separate arguments with commas. They're passed on every Run as sys.argv[1:].