r/PythonLearning • • Aug 17 '25

What's wrong

Post image
49 Upvotes

45 comments sorted by

View all comments

3

u/earchip94 Aug 17 '25 edited Aug 18 '25

Length of a list/array/etc is not a valid index for that object. Because it is 0 indexed you at a minimum need to subtract 1 from the length. Range syntax and usage is valid. Max value of range(n) is n-1.

Edited for clarity.

2

u/AwkwardBet5632 Aug 17 '25

range(n) goes from 0 to n-1.

2

u/earchip94 Aug 17 '25

Yes it does but they are using n as the index for every iteration. I don’t think that’s what is intended but the exception is because of their usage of n.