Space Python Loading Python…

Last five readings

Write the codeEasy 2 stages

The cabin CO2 sensor sends a reading every minute, and the list of readings only grows. The crew's dashboard doesn't want all of them: it shows the five most recent, so a change is easy to spot.

Slice out the latest five, then keep the window moving as new readings arrive.

The dashboard only shows the five most recent readings. Slice them out of the list and print them:

Dashboard: [701, 688, 720, 745, 760]

The list is oldest first, so the most recent are at the end. A negative start counts from the end, as with strings.

Show the latest five
Works for any number of readings

Finish stage 1 to unlock this stage.

A new reading has arrived. Add new_reading = 772 under the list, append it to the readings, then show the new window and how much the level changed between its first and last reading, with a sign:

Dashboard: [688, 720, 745, 760, 772]
Change over the window: +84 ppm

{change:+d} prints a whole number with its sign, +84 or -38.

Move the window
Works for any new reading
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

List Indexing and Modification

list-indexing-modification.py
# Lists - reading and changing items by position

# Galilean moons, ordered by distance from Jupiter
moons = ["Io", "Europa", "Ganymede", "Callisto"]

# Index 0 is the first item, not 1
print("Closest:", moons[0])
print("Second:", moons[1])

# Negative indexes count back from the end
print("Farthest:", moons[-1])
print("Last index:", len(moons) - 1)

# Slicing: start included, stop excluded
print("Next stops:", moons[1:3])

# Apollo 13: Mattingly was grounded 3 days before launch
crew = ["Lovell", "Mattingly", "Haise"]
crew[1] = "Swigert"
print("Flight crew:", crew)

# append() adds one item to the end
moons.append("Valetudo")
print("After append:", moons)

# remove() searches by value, not by index
moons.remove("Io")
print("After remove:", moons)

# Every position shifted down by one
print("New first:", moons[0])
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:].