Another modify task, and this time a loop picks out the items.
A fitness app stores a week’s step counts for three users in a 2D array. The first item on each row is the user’s name, followed by three daily step totals.
The program is supposed to add up the steps for the user on row 1, Mary, by
looping through her step data, and then print her name with her total. The
editor starts you off with this:
steps = [
["Bob", 5000, 6000, 5500],
["Mary", 8000, 9000, 8500],
["James", 4000, 3000, 4500]
]
total = 0
for i in range(1, 4):
total = total + steps[1][1]
print(steps[0][0] + " walked " + str(total) + " steps")
Run it first. It prints Bob walked 24000 steps, which is wrong twice over:
the steps are Mary’s, but the name is Bob’s, and the total is Mary’s first day
added up three times.
Two indices are wrong. Change only those two.
print, so that it prints Mary’s name.The tests also check your code: a program without the for loop does not pass.
One line:
Mary walked 25500 steps
Make sure that you check for the following things:
i takes the values 1, 2 and 3. Those are exactly the
columns that hold step counts.