# Iterating over a list in a function interesting error

n = [3, 5, 7]

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

btw how to write codes like:
def aaa(b):
___print"thx"

4 Likes

`n = [3, 5, 7] def total(numbers): result = 0 for i in range(len(numbers)): result +=i return result`
problem:
Oops, try again. total([0, 3, 6]) returned 3 instead of 9

1 Like

Just return `sum(numbers)`, simple solution.

6 Likes

But why original code doesnâ€™t work?

1 Like

I was stuck on this problem for a long time but I realized that my issue was incorrect indentation. If your code looks correct, try playing around with the indentation. For me, it was the indentation for the result and return line on lines 6 and 7.

Just in case you need it, hereâ€™s my code below:

8 Likes

`print total(n)` is needed at the end of your code

1 Like

n = [3, 5, 7]

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

This is the shortest I believe!

3 Likes

it works here

n = [3, 5, 7]
def total(numbers):
result = 0
for i in numbers:
result +=i
return result
print total(n)

or use
for i in range(len(numbers)):
result +=numbers[i]

1 Like

i often forgot the indentations. thanks for the tips.

``````n = [3, 5, 7]

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

and my code works with/without the â€śprint total(n)â€ť (?)

1 Like

That is also right but they want him in that section to learn how to utilise for loops in functions.

1 Like

n = [3, 5, 7]

# print total(n)

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

# print total(n)

1 Like

1 Like

this isnâ€™t working for me
it gives the following error:
Oops, try again. total([0, 3, 6]) returned 0 instead of 9

1 Like

I realise the tutorial accepts one answer, and one only, even when the alternate answer is right according to the console. But why would the following not work:

def total(numbers):
____for i in range(len(numbers)):
________if len >= 2:
____________i[0] == i[0] + i[1]
____________del(i[1])
________elif len == 1:
________return i[0]

print total(n)

Thank you

1 Like

Look at the code and find the difference:

n = [3, 5, 7]

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

``return result``
1 Like

How does posting random code and saying,

Help the conversation in any way?

1 Like

programing is about analysis if you donÂ´t think an understand what one program those and a little change those another thing you will never learn get it!! it my thinking.

Thanks for u ,as u say ,I do this ,Itâ€™s worked

n = [3, 5, 7]

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

But this step is not workde (i think here is the indent problem )

n = [3, 5, 7]

def total(numbers):
---- result=0
---- for i in range(len(numbers)):
--------result+=numbers[i]
---- ----return result # left indent text 4 spaces is worked
print total(n)

like the flow step is all right:

n = [3, 5, 7]

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

1 Like

One personâ€™s method is not necessarily anotherâ€™sâ€¦ programming is formulating and implementation of a program that can resolve an issue, that may involve analysis, it may involve analysis of code to fix it, but not posting of code to the tune of â€śWhereâ€™s Waldoâ€ť.

Vague and ambiguous posts may be fine for some, but a well explained posts clarifying a push in the general direction may be more beneficial to some.

Your methodology is and may be different than others. Your workflow may be different than others. See the difference?

1 Like