0

So first of all im writing a program that will total up all of the values in a list / array and heres what i got so far

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

print ( go( [-99,1,2,3,4,5,6,7,8,9,10,12345] ) )
print ( go( [10,9,8,7,6,5,4,3,2,1,-99] ) )
print ( go( [10,20,30,40,50,-11818,40,30,20,10] ) )
print ( go( [32767] ) )
print ( go( [255,255] ) )
print ( go( [9,10,-88,100,-555,1000] ) )
print ( go( [10,10,10,11,456] ) )
print ( go( [-111,1,2,3,9,11,20,30] ) )
print ( go( [9,8,7,6,5,4,3,2,0,-2,-989] ) )
print ( go( [12,15,18,21,23,1000] ) )
print ( go( [250,19,17,15,13,11,10,9,6,3,2,1,-455] ) )
print ( go( [9,10,-8,10000,-5000,1000] ) )

now what i may belive is wrong is probably my code and has to do something with the range function? anything will help on why i keep getting 'return' outside function

I have tried looking at other post but they didnt help too much

0

2 Answers 2

1

Your indentation is incorrect - The loop statement should be indented in the same line as other statements and the loop body should be indented one level inside the loop statement. The return statement will have the same indentation level as the other statements.

Try this:

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

print ( go( [-99,1,2,3,4,5,6,7,8,9,10,12345] ) )
print ( go( [10,9,8,7,6,5,4,3,2,1,-99] ) )
print ( go( [10,20,30,40,50,-11818,40,30,20,10] ) )
print ( go( [32767] ) )
print ( go( [255,255] ) )
print ( go( [9,10,-88,100,-555,1000] ) )
print ( go( [10,10,10,11,456] ) )
print ( go( [-111,1,2,3,9,11,20,30] ) )
print ( go( [9,8,7,6,5,4,3,2,0,-2,-989] ) )
print ( go( [12,15,18,21,23,1000] ) )
print ( go( [250,19,17,15,13,11,10,9,6,3,2,1,-455] ) )
print ( go( [9,10,-8,10000,-5000,1000] ) )
Sign up to request clarification or add additional context in comments.

Comments

1

Indentation is the problem here. Always remember indentation is important in Python. Your for loop is not indented properly, i.e. it's outside the function where your total variable is declared.

def go( ann ):
    total = 0

    for i in range(0,len(ann)): #first indentation mistake fixed
        total = total+ann[i] #second indentation mistake fixed
    return total

From your print statements, the output should be:

12301
-44
-11568
32767
510
476
497
-35
-947
1089
-99
6011

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.