Example
For Loop with enumerate and range
Functions enumerate() and range() extend for loop capabilities in Python.
Showing 1–10 of 21
Example
Functions enumerate() and range() extend for loop capabilities in Python.
Challenge
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.
Quiz
…quiz on Python for loops: 15 questions on lists, strings, range(), enumerate(), totals and break, with instant answers.
Challenge
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.
Challenge
…last line is Uplink failed after 3 attempts . Loop over range(1, MAX_ATTEMPTS + 1) , break on success, and use a flag such as sent to tell the two endings apart. Back off between tries Retrying at…
Challenge
…if the wind is under 30 km/h, there is no lightning and range safety reports GO, using and and not, then print every rule that is holding the launch.
Challenge
…over a copy of the defaults, check each value's type and range and report every problem in one go, then wrap it all in a load_config() that never crashes, even on broken JSON.
Challenge
…the longitude under a spacecraft with an if/elif chain of ranges, then accept longitudes from 0 to 360 and reject values that can't be longitudes.
Challenge
…loops around: the outer loop goes over the column numbers, range(len(grid[0])) , and the inner loop over the rows, checking line[column] . A flag says whether the column was blocked.
Challenge
…a syntax error, a type error, a wrong key, an index out of range and a reversed decision. Read each error to fix the four that crash, then find the one that runs but gives the wrong answer.