Iterating over a list in a function



What am I doing wrong? I'm kinda stuck :rage:

Here is the link - - - - - - - - - - - - - - - - - - - - - - - - :arrow_down:

This is the error:

 Oops, try again. Your code looks a bit off. Check the console window for errors!

and here is the console:

  File "python", line 6
    for number in numbers
SyntaxError: invalid syntax

and here is the code ------- :arrow_down:

n = [3, 5, 7]

def total(numbers):
    result = 0
    numbers = [1, 9, 6, 10]
    for number in numbers 
    result + 1
    return result


Add a colon after the for loop
( for number in numbers: )


You're indentation is off. Also, you add one to result but then you don't store it anywhere.


for result, try;
result +=number


Alright, once you fix the colon, you'll have this.

for num in range(0, len(numbers)):
(indent) result += numbers[num]
return result

When using range method, you have to remember that you are dealing with index(or indices in plural). Thus, you have to include the index in a list, to access it


Error: :arrow_down:

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

Code: :arrow_down:

def total(numbers):
    result = 0
    numbers = [1, 9, 6, 10]
    for number in numbers:
        result += number
    return result


Hmm, I added a range() into it... still not working :rage:


That seems fine. Just delete numbers = [1, 9, 6, 10]. You are basically outputting ONLY 26 if you had that in.


Your number in your for loop is an index for the list, numbers. So you need to add it into result += number like this:

result += numbers[number]


Don't add any lists. Use the one given. It will work


Here's how I did it.


Thank you. I was banging head against wall until you screen shot made me realize I had the indent on the return too far over.


I'm not gonna steal your code mate :slight_smile:


Okay fine... I stole your code because I'm stuck for so long :rage:


But not all of you code :slight_smile:


Thanks for the help
you really helped a lot :') THANKS
You helped me (a little bit) thanks :slight_smile:

To all those that helped me THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!! :slight_smile:


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