3.7.3.2. Exercise: Looping using for and range 2#

Redo Exercise: Looping using while 1, this time with a for instead of a while:

Using an instrumented walkway, we recorded the following positions of heel strike, first for the right heel, second for the left heel, third for the right heel, etc., according to Fig. 3.7.

# y-coordinates of each heel strike, in meters
y = [0.13, 0.72, 1.29, 1.93, 2.55, 3.12, 3.71, 4.34, 4.95, 5.56]

Write a program that creates a list named step_lengths, that contains the length of every step. The first step length would be y[1] - y[0], the second would be y[2] - y[1], etc. In this specific example, we recorded nine steps, but your code must work on any number of steps.

Hide code cell content
# y-coordinates of each heel strike, in meters
y = [0.13, 0.72, 1.29, 1.93, 2.55, 3.12, 3.71, 4.34, 4.95, 5.56]

# Initialize an empty list to put the results of our calculations
step_lengths = []

# Count the number of steps in this set of data
n_steps = len(y) - 1

# Calculate the step lengths
for i in range(n_steps):
    step_lengths.append(y[i + 1] - y[i])

# Done!
step_lengths
[0.59,
 0.5700000000000001,
 0.6399999999999999,
 0.6199999999999999,
 0.5700000000000003,
 0.5899999999999999,
 0.6299999999999999,
 0.6100000000000003,
 0.6099999999999994]