因此,首先,我编写了一个程序,它将汇总列表/数组中的所有值,如下所示
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] ) )现在,我可能认为错误的可能是我的代码,必须用范围函数做些什么?任何东西都能帮助我为什么要继续得到'return' outside function
我试过找其他职位,但他们帮不了什么忙
发布于 2021-03-31 04:40:56
您的缩进是不正确的-循环语句应该缩进与其他语句相同的行中,循环主体应该缩进循环语句中的一个级别。返回语句将具有与其他语句相同的缩进级别。
试试这个:
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] ) )发布于 2021-03-31 04:52:49
压痕是这里的问题。永远记住缩进在Python中是很重要的。您的for循环没有正确缩进,即它位于声明您的总变量的函数之外。
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从打印语句中,输出应该是:
12301
-44
-11568
32767
510
476
497
-35
-947
1089
-99
6011https://stackoverflow.com/questions/66881440
复制相似问题