15/18 Whats Wrong


#1

n = [3, 5, 7]

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


#2

You dun goofed.

n = [3, 5, 7]

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

where it says numbers[i] = numbers[i] + result, it SHOULD BE result += numbers[i] (use your method for increment I like +=)

Working code :

n = [3, 5, 7]

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

return result

#3

For Method 1

n = [3, 5, 7]

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

print total(n)

For Method 2

n = [3, 5, 7]

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

print total(n)

#4

Please mark your Q/A as solved, thanks in advance :smile:


#5

thx tots got it without you life would have been over...
thx selfie xx


#6

May I please know what does the "+=" in result += numbers [i] mean?


#7

you should notice this in for structure: result = numbers[i] + result, not numbers[i] = numbers[i] + result


#8

The result += numbers [i] is just a shortcut for result = result + numbers [i]
it allows you to save some time, and makes your code smaller.


#9

return result should not be in the for loop


#11

Why is there a "0" on the fifth line of Method 2? :confused: