15. Iterating over a list in a function


#1

Ok my problem is that there is mentioned that you can use either of the 2 methods in this exercise, but when i use the method 2 i get an error that is "Oops, try again. total([0, 3, 6]) returned 3 instead of 9". But when i use method 1 it goes all well and doesnt show any error, why is that?

Code 1:(method 2)

n = [3, 5, 7]

def total(numbers):
    result = 0
    for number in range(len(numbers)):
        result = number + result
    return result
print total(n)

Output 1:
3

Code 2:(method 1)

n = [3, 5, 7]

def total(numbers):
    result = 0
    for number in numbers:
        result = number + result
    return result
print total(n)

Output 2:
15


#2

because range gives you indexes:

def total(numbers):
    result = 0
    for number in range(len(numbers)):
        print number
print total(n)

you can then use this indexes to access the items in the list:

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

#3

Then how to use this in this particular example, because if i use the code you provided it still gives out error "Oops, try again. total([0, 3, 6]) returned 0 instead of 9"

Code:
n = [3, 5, 7]

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

print total(n)


#4

no, with numbers[number] you have no access to the numbers in the list, now you can add it to result and then return result

I just showed you how to use indexes to access items in a list, there was still work for you to do


#6

I was able to complete this exercise with the codes provided. What I don't understand is, what is tiing the list (n) to the function (total)? I know it works, just not why. Thank you for your help


#7

please clarify your question with code


#8

I know you figured it out, but you can have it iterate through lists with mathematical functions without it returning anything for each step. Here's what worked for me.

n = [3, 5, 7]

def total(numbers):
result = 0
for items in numbers:
items + result
result = result + items
return result
print total(n)


#9

for some reason i am getting an error when running this code:

n = [3, 5, 7]

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

Oops, try again. total([0, 3, 6]) resulted in an error: 'int' object has no attribute 'getitem'

anyone able to shed some light on what im doing wrong?


#10

I was stuck too, but looking at your code acutally helped me. I switched:
number[numbers] to numbers[number] and it works


#11

@bigp172006 solution should help you?


#12

The below code also doesnt seem to work
Code:

n = [3, 5, 7]

def total(numbers):
    result = 0
    for number in range(len(numbers)):
        numbers[number] + result
        result = result + numbers[number]
        return result
print total(n)

#13

this line:

result = result + numbers[number]

will update result, so this line:

numbers[number] + result

serves no purpose, remove it.

A function ends the moment a return keyword is reached, this currently is happening in the first iteration of your loop, change the indent of return so the whole for loop can run


#14

Yeah, i did what you said and all the problem was wrong indent of return. This code worked thanks to you..
Code:
n = [3, 5, 7]

def total(numbers):
result = 0
for number in range(len(numbers)):
result = result + numbers[number]
return result
print total(n)


#15