+ 1
Range question: correct answer seems wrong.
A range is created to 5, and then the 4th list item is printed, and the correct answer is 4?!? Shouldn't it be 3 since 3 is the 4th element of the list? The test question on this app only accepted 4 as correct.
4 ответов
+ 1
I looked in the Python course and I think you are talking about this question...
nums = list(range(5))
print(nums[4])
range(5) is equal to range(0, 5) so when it's turned into a list it returns [0, 1, 2, 3, 4]. When accessing an element from a list it uses indexes that start from 0 so
1st element = 0 index
2nd element = 1 index
...
So the 4th index (nums[4]) is the 5th element in the list which is 4.
0
Thank you so much! I remembered that the range started at 0 but forgot the index also started at zero. I figured I was being a knucklehead. Thank you so much for the help.
0
You're welcome, I would recommend to next time put the language in the tags or description and provide a link to the course section.
0
Ok, I'll do that. I thought this forum was actually just a log of questions created by users, and was specific to the section you're in when you hit the Q&A icon on the top. I know better now.