Need help with iterating through indexes. thank you


#1



https://www.codecademy.com/en/courses/python-beginner-nzzVa/3/4?curriculum_id=4f89dab3d788890003000096


Hi, I am attempting to do this exercise using "Method 2 - iterate through indexes" and not by using "Method 1 - for item in list" because ive had more practice with the first method. but I am stuck now and need some help. please tell me what I have to add to the code and explain why please


n = [3, 5, 7]

def total(numbers):
    result = 0
    for i in total(


#2

if we need to use indexes we need to use range() (docs - range), the range should start at 0 (first index) till the last item of the list (len(n)), which gives us:

for i in range(0,len(numbers)):
   print i

as you can see, i now gives us indexes which we can use to access the items in the list (this notation should look familiar):

for i in range(0,len(numbers)):
   print numbers[i]

we use i to access the item from the numbers list


#3

hi thanks for replying, i'm still a little confused, i put everything in like this :

n = [3, 5, 7]

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

and I'm getting the error 'Oops, try again. total([0, 3, 6]) returned None, did you remember to return the result? '


#4

uhm... i just showed you how to to use indexes to access the values? You might want to add a function call:

n = [3, 5, 7]

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

you still have to add all the numbers from the list to total, and return it. You asked for help with iterating, that is what i gave you? First try if you can figure out how to add them to total, if you need more help post an updated version of your code


#5

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