3. Lists and Loops

Store many values in order. Do the same thing to all of them without copy-pasting.

Summary

A list keeps values in order:

currents = [38.2, 41.0, 39.5, 31.4]

Tools for lists:

  • Read one value with an index. Indexes start at 0: currents[0] is 38.2.
  • currents[-1] reads the last value: 31.4.
  • An index that is too big gives an IndexError.
  • len(currents) gives the count: 4.
  • currents.append(40.1) adds a value to the end.

A loop visits each value:

for c in currents:                  # each value in the list
    print(c)

for i in range(4):                  # i = 0, 1, 2, 3
    print(i)

for i, c in enumerate(currents):    # index and value together
    print(f'module {i}: {c} A')

tick = 0
while tick < 100:                   # repeats while the condition is true
    tick += 1

Which one to use:

  • for x in list: you need the values. Most common.
  • for i in range(n): you need to count, or index into the list.
  • for i, x in enumerate(list): you need the index and the value.
  • while: you do not know how many repeats. Be careful: a wrong condition loops forever.
  • break inside a loop exits it at once.

In robot code, do not use a bare while to wait for time to pass. The robot runs its loop 50 times per second, and a blocking while stops all of it. Waiting uses coroutines with yield (lesson 6).

In our robot code

robot/robot.py registers every subsystem with one loop instead of copy-pasted lines:

self.subsystems = [
    self.drivetrain, self.poseEstimator,
    self.intake, self.shooter, self.hopper,
]
for subsystem in self.subsystems:
    self.scheduler.registerSubsystem(subsystem)

The hopper fuel simulation uses range:

for fuel_num in range(1, self.max_fuel_in_hopper + 1):
    ...

Try it

Try it
Try it: for and enumerate
✓ solved

Run the code. Add 'climber' to the list. Run again.

The code operates in your browser. The page sends nothing to a server and saves your work on this computer. Press Ctrl+Enter to run it.

Exercises

3.1 Total current and hot modules

Try it
Total current and hot modules
✓ solved

currents holds the amps of the four swerve modules.

PART A: add every current into total with a for loop. Do not use sum().

PART B: count the modules that use more than 40 amps into hot_count.

The code operates in your browser. The page sends nothing to a server and saves your work on this computer. Press Ctrl+Enter to run it.

3.2 Find the module with the most current

Try it
Find the module with the most current
✓ solved

A swerve module that draws much more current than the others often has a mechanical problem. Find it.

PART A: set best_index to the index of the module with the most current.

PART B: set best_current to that module’s value.

The code operates in your browser. The page sends nothing to a server and saves your work on this computer. Press Ctrl+Enter to run it.

3.3 Count loop steps and calculate nominal time

Try it
Count loop steps and calculate nominal time
✓ solved

This is a loop-step model. Assume each real robot cycle has a nominal period of 0.02 seconds (20 ms).

PART A: use a while loop to increase tick from 0 to 100.

PART B: set seconds to tick * 0.02. This calculates the nominal time for 100 robot cycles under that assumption.

The browser loop runs as fast as Python permits. It does not wait 20 ms per step or measure elapsed time. In robot code, use the scheduler and yield to let other work continue. A yield alone does not measure time.

The code operates in your browser. The page sends nothing to a server and saves your work on this computer. Press Ctrl+Enter to run it.

Next lesson: 4. Functions.