Answer the question
In order to leave comments, you need to log in
Why does python ignore the first value of a list?
This is from exercise.
list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sum = 0
mult = 0
for sum in list:
if(sum<9):
mult = mult + list[sum]
sum = sum+1
else:
print(mult)
Answer the question
In order to leave comments, you need to log in
for sum in list
- this is a search of the elements of the list in order;
mult = mult + list[sum]
this is an appeal to the element of the list by index. The first element is 1, which means it will be the second by index, because the numbering starts from zero. - it makes no sense, this value will be reset at the beginning of the loop, because there is a iteration of sum from the list
sum = sum+1
And you don't have list[0] there, your "sum in list" is 1, 2 and so on. That is, sum is not an index.
Run, you will understand what I mean:
list = [123, 277, 783, 8974, 75, 78676, 147, 7828, 9099]
sum = 0
for sum in list:
print(sum)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question