r/PythonLearning • • Aug 17 '25

What's wrong

Post image
49 Upvotes

45 comments sorted by

View all comments

1

u/Wrong-Bit8534 Aug 17 '25

Array starts from 0, try n-1

4

u/AwkwardBet5632 Aug 17 '25

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

1

u/Swipsi Aug 17 '25

An array with 3 elements will give len() = 3

Range(3) will give you 0, 1, 2, 3

Using range for the loop will loop 4 times, starting with index 0 and throw an index out of array exception at index 3 because the array only has 3 elements.

1

u/AwkwardBet5632 Aug 17 '25

range is non inclusive of the stop value