Space Python Loading Python…

sort() returns None

Fix the codeEasy 3 stages

The duty roster should list the crew alphabetically. One line of code was supposed to sort it, and since that line went in, the roster says None.

Predict what it prints, find out where the crew went, fix it, then sort a list of readings yourself.

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.

Follow crew through the sort and into the print.

Predict, then run
Leave the program as it is

Finish stage 1 to unlock this stage.

Investigate. Find out what crew.sort() gives back and store it, as text, in sort_returns. Then sort on a line of its own, print crew[0], and store that name in first_after_sort. Remove any lines you added before you check.

Modify. Fix the sort so the roster is sorted and kept. It must print:

Roster: ['Kowalska', 'Lindqvist', 'Okafor', 'Tanaka']
Investigate sort()
Keep the sorted roster

Finish stage 2 to unlock this stage.

Sort the engine readings from highest to lowest, in place, with sort(reverse=True), then print the three highest with a slice:

Highest three: [455.2, 430.1, 412.5]

Don't store what sort() gives back.

Print the top three
Sort in place
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

Sorting and Reversing Lists

sorting-reversing-lists.py
# Sorting: change the list, or build a new one

# The first five asteroids ever found, in discovery order
found = ["Ceres", "Pallas", "Juno", "Vesta", "Astraea"]
diameters = [939, 512, 247, 525, 119]  # km, same order
print("Discovery order:", found)

# sort() rearranges the list itself, smallest first
diameters.sort()
print("Smallest first:", diameters)

# reverse=True flips the direction of the sort
diameters.sort(reverse=True)
print("Largest first:", diameters)

# reverse() does not sort, it only flips what is already there
found.reverse()
print("Newest first:", found)

# sorted() builds a new list and leaves the original alone
alphabetical = sorted(found)
print("Alphabetical copy:", alphabetical)
print("Original still: ", found)

# A common bug: sort() returns None, so this would erase your list
# diameters = diameters.sort()
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:].