15. Iterating over a list in a function


#1

I'm trying to solve this ■■■■ 15 section during a day, and it doesn't pass. The problem is "Oops, try again. total([0, 3, 6]) returned 0 instead of 9".

Here's the code:
n = [3, 5, 7]
def total(x):
result = 0
for i in range(0, len(x)):
result = result + x[i]
return result

Any suggestions and solutions would be great.


#2

A quick fix will be to do:

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

#3

it still writes "Oops, try again. total([0, 3, 6]) returned 0 instead of 9"

Code:

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


#4

You need to format your code so I see it clearly.

The code works fine. See here: 15. Iterating over a list in a function


#5

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

It looks like this, but still the mistake "Oops, try again. total([0, 3, 6]) returned 0 instead of 9"


#6

and in https://repl.it/Cgxk/3 it gives me the number 3 instead of 9


#7

Please check your indentation. Just hit the enter key once after a semi-colon. See screenshot.


#8

Your tampered with the indentation.

This language is indentation sensitive. :slight_smile:


#9

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

#10

Yeah I've understood that :grinning: And solved the problem. This ■■■■ indentation. Thx man!:sunglasses:


#11

You're welcome! :slight_smile:


#12

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