MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/PythonLearning/comments/1msspug/whats_wrong/n97uu7d/?context=3
r/PythonLearning • u/Nearby_Tear_2304 • Aug 17 '25
45 comments sorted by
View all comments
1
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() = 3Range(3) will give you 0, 1, 2, 3Using 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
4
range(n) goes from 0 to n-1.
1 u/Swipsi Aug 17 '25 An array with 3 elements will give len() = 3Range(3) will give you 0, 1, 2, 3Using 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
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
range is non inclusive of the stop value
1
u/Wrong-Bit8534 Aug 17 '25
Array starts from 0, try n-1