为什么会是这样的结果啊
def square_of_sum(L):
sum=0
for x in L:
sum=sum+x*x
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
==>1
25
def square_of_sum(L):
sum=0
for x in L:
sum=sum+x*x
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
==>1
25
2019-09-27
def square_of_sum(L):
sum=0
for x in L:
sum=sum+x*x
return sum(这里错了这样写的话for循环在迭代第一个元素之后就结束了,return是结束循环返回一个值)
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
改成这样
def square_of_sum(L):
sum=0
for x in L:
sum=sum+x*x
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
举报