Iterating over a list in a function


#1



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


Oops, try again. total([0, 3, 6]) returned 15 instead of 9

Heading

What is the mistake? :sweat:


I made a function which returns the sum of numbers .


n = [3, 5, 7]

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


#2

Your for loop:

for i in range(len(n)): #n is not your function parameter which is numbers 
    result=n[i]+result #but your local list. Change n to numbers

#3

It is right.
thank you a lot.


#4

You're welcome. Have a great day :slight_smile:


#5

:angry:
n = [3, 5, 7]

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

It didnt make any change if I wrote result = result + numbers(i)
error message: Traceback (most recent call last):
File "python", line 8, in
File "python", line 6, in total
TypeError: 'list' object is not callable
This message doesnt help me a *****


#6

my last text was presented here without indentation
my code was correctly indented so this is not the case


#7

Sjurjo - you have round brackets on your numbers[i] instead of square brackets. Otherwise everything looks fine. See mine below:

n = [3, 5, 7]

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


#8

Hello Alison

Thanks for your help. Yes you are correct. I already have found a solution
and moved futher. As I remember, my final version was a little bit
different but sometimes many different solutions work fine.

Silvester
22.08.2016 11:21 kirjutas kuupäeval "Alison Williams" <
codecademy@discoursemail.com>:

megamaster63793 https://discuss.codecademy.com/users/megamaster63793
August 22

Sjurjo - you have round brackets on your numbers[i] instead of square
brackets. Otherwise everything looks fine. See mine below:

n = [3, 5, 7]

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

Visit Topic


or reply to this email to respond.
In Reply To
sjurjo https://discuss.codecademy.com/users/sjurjo
August 19
:angry: n = [3, 5, 7] def total(numbers): result=0 for i in
range(len(numbers)): result += numbers(i) return result print total(n) It
didnt make any change if I wrote result = result + numbers(i) error
message: Traceback (most recent call last): File "python", line 8, in …

Visit Topic


or reply to this email to respond.

To unsubscribe from these emails, click here
https://discuss.codecademy.com/email/unsubscribe/1e4a79e9b8d43f677c607ee9d1e0cdfa23744c1403d72d833a1746a1334b3f45
.


#9
n = [3, 5, 7]

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

#10

dear mahmoudelnazer

this is your answer:

n = [3, 5, 7]

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

#11

n = [3, 5, 7]

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


#12

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