Iterating over a list in a function interesting error


#1

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"


#2

What is your problem/what are you trying to do. Also, please format your code.


#3

i didnt know you could post your code properly indented .... thanks :smiley:


#4


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


#5

Just return sum(numbers), simple solution.


#6

But why original code doesn't work?


#7

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

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


#9

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!


#10

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]


#11

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)" (?)


#12

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


#13

n = [3, 5, 7]

def total(numbers):

result = 0

for i in numbers:

result += i

return result

print total(n)

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

def total(numbers):

result = 0

for i in range(len(numbers)):

result += numbers[i]

return result

print total(n)


#14

three methods, for your information.


#15

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


#16

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 :slight_smile:


#17

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

#18

@byteace65303,

How does posting random code and saying,

Help the conversation in any way?


#19

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.


#20

Thanks for u ,as u say ,I do this ,It's worked:grin:

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::laughing:

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)