Space Python Loading Python…

Off-by-one slice

Fix the codeEasy 3 stages

The cargo system reads the number out of each container tag, like CG-0042, to look the container up in the manifest. Container 42 never shows up, and the log is full of numbers that start with a dash.

Predict what the slice gives, work out where it goes wrong, fix it, then slice a sample tube label 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.

Count the positions in CG-0042 carefully before you guess.

Predict, then run
Leave the program as it is

Finish stage 1 to unlock this stage.

Investigate. Store the index of the dash in dash_index and the index of the last character in last_index. Check them with the prints in the comments, and remove those lines before you check.

Modify. Fix the slice so it takes the four digits after the dash. It must print:

Container number: 0042
Count the positions
Fix the slice

Finish stage 2 to unlock this stage.

Sample tube labels look like SMP-2026-0317-A: SMP, the year, a sequence number and a batch letter. Slice the label to print its parts:

Year: 2026
Sequence: 0317
Batch: A

Count the positions before you write each slice, and remember that the end index isn't included. For the last character, a negative index saves you counting.

Slice the label
Works for any label
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

String Indexing and Slicing

string-indexing-slicing.py
# String indexing - every character has a position, counting from 0
mission = "ARTEMIS"
#          0123456

# Grabbing individual characters
print("First letter:", mission[0])    # A
print("Last letter:", mission[-1])     # S

# Slicing [start:stop] - stop is NOT included
print("First three:", mission[0:3])    # ART
print("From index 4 on:", mission[4:])  # MIS
print("Last three:", mission[-3:])      # MIS

# Step slicing [start:stop:step]
launch_code = "A1B2C3D4"
print("Every 2nd:", launch_code[::2])   # ABCD
print("The digits:", launch_code[1::2]) # 1234

# Reversing with a negative step
print("Reversed:", mission[::-1])       # SIMETRA
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:].