Space Python Loading Python…

Missing index

Fix the codeEasy 3 stages

The engine monitor keeps the last five readings and shows the latest one on screen. The developer counted five readings, wrote readings[5], and the monitor crashes every time it starts.

Predict what happens, find out which indexes exist, fix it, then pick out readings at either end and in the middle.

Don't run the code yet. Read it and decide what it will print. 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.

Count the readings, then count the indexes.

This program crashes, and a program that crashes can't be checked. After you run it and see the error, put a # in front of the print line, then check: you'll see your prediction next to what the original line did.

Predict, then run

Finish stage 1 to unlock this stage.

Investigate. Store the number of readings in reading_count and the index of the last one in last_index.

Modify. Print the latest reading in a way that still works when the list is longer or shorter. It must print:

Latest reading: 430.1
Count the indexes
Print the latest reading

Finish stage 2 to unlock this stage.

Print the first reading, the one in the middle and the last one:

First: 412.5
Middle: 455.2
Last: 430.1

The middle one is at index len(readings) // 2: 2 for five readings, 3 for seven. Everything has to work for any number of readings.

Print three readings
Works for any number of readings
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:].