15/18 - int not iterable


#1
n = [3, 5, 7]
def total(numbers):
  result = 0
  for i in numbers:
    result = sum(numbers[i])
  return result

gives me error: int not iterable

another solution worked but not sure I am getting the difference

n = [3, 5, 7]
def total(numbers):
  result = 0
  for i in numbers:
    result += i
  return result

#2

sum() calculates the sum of an iterable like a list. numbers[i] is a integer. So numbers[i] is not a valid argument for sum()

besides, for i in numbers will give values of the list, not indexes. so numbers[i] might result in index error if there is a value in the list which is higher then the highest index of the list


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.