Space Python Loading Python…

For Loop with enumerate and range

 
0:00 / 0:00

Challenge

Practise what this example shows with these challenges.

Numbered checklist

Write the codeEasy 2 stages

Print a pre-flight checklist numbered from 1 with enumerate(list, start=1), then tick off the items the crew has already done with [x] and leave the rest as [ ].

Start the challenge

One orbit short

Fix the codeEasy 3 stages

A loop over range(1, 10) logs nine orbits of a ten-orbit flight. Predict the last line, find out where range() stops, fix it with ORBITS + 1, then list every third orbit with a range() step.

Start the challenge

Countdown with holds

Write the codeEasy 2 stages

Print a launch countdown from T-10 to T-0 with range(10, -1, -1), adding a hold line at every second listed in holds, then scrub the launch at a given second with break, or print Liftoff! at the end.

Start the challenge

Names with values

Write the codeEasy 2 stages

Print each sensor name with its reading by looping over two lists together with zip(), then add a third list of units and warn when the lists don't have the same length, since zip() quietly stops at the shortest.

Start the challenge

Debris grid

Write the codeMedium 2 stages

Scan a radar grid stored as a list of strings with a loop inside a loop, print the row and column of every # with enumerate(), then turn the loops around to find the columns that are clear all the way through.

Start the challenge

Shift rota

Write the codeHard 2 stages

Build the station's weekly shift rota: three shifts a day, the crew taken in turn so nobody works two shifts in a row, with a check that proves it, then plan around days off while keeping the work fair.

Start the challenge

Discussion

Stuck on this example or want to go further? Ask here, or see what others asked.

Log in to ask and reply.

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:].