Most loops you will write do one of two things: they visit every item, or they accumulate a result across every item.
total = 0
for price in prices:
total = total + price
Three things are happening, and beginners routinely merge them:
total = 0— the accumulator starts at a value that is correct for an empty list. For a sum that is0; for a product it is1; for “the largest so far” there is no safe starting value and you have to handle the empty case.for price in prices— one pass, one item at a time.total = total + price— the update. Read the right-hand side first: it uses the old total.
The two off-by-one errors
Starting inside the loop. Putting total = 0 inside the for resets it
every time, so you finish with only the last item. The program runs and the
answer is wrong, which is the worst combination.
Using range(len(...)) when you wanted the items. range(len(prices))
gives you 0, 1, 2, not the prices. If you want the items, loop over the list.
If you genuinely need positions as well, use enumerate.
Exercise
Write longest_word(words) returning the longest string in a list. Decide what
it should do for an empty list before you write it, and write that decision as
a comment. Then make the code match the comment.