13/18 - why the "i" in the loop?


#1

https://www.codecademy.com/courses/learn-python/lessons/lists-and-functions/exercises/iterating-over-a-list-in-a-function?action=lesson_resume&link_content_target=interstitial_lesson


n = [3, 5, 7]

def total(numbers):
  result = 0
  for i in range(0,len(numbers)):
    result += numbers[i]
  return result

Why does the solution ask for an “i”? Shouldn’t it be an “n” as the list it’s calling from?


#2

actually not, lets add some function calls so we can see it better:

n = [3, 5, 7]

def total(numbers):
  result = 0
  for i in range(0,len(numbers)):
    result += numbers[i]
  return result

print total(n)
print total([2,4,8])

you pass the list n as argument on function call, so value of n get passed to the function parameter numbers

using n for the loop iterator would be confusing, then it would be challenging to remember which is the list and which the loop iterator. Now in a program this small, its maybe a bit less relevant.

i am slightly confused, most people confuse n and numbers, not n and i


#3

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