这段代码有什么问题?
def square_of_sum(L):
i = 0
sum = 0
while i in len(L):
L[i] = L[i]*L[i]
i = i+1
sum = sum +L[i]
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
def square_of_sum(L):
i = 0
sum = 0
while i in len(L):
L[i] = L[i]*L[i]
i = i+1
sum = sum +L[i]
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2019-10-23
def square_of_sum(L):
i = 0
sum = 0
while i in len(L): // 此处应为 i < len(L)
L[i] = L[i]*L[i]
i = i+1 // 索引值的变更应该在sum + L[i]之后,否则获取的是下一个索引位置的值
sum = sum +L[i]
return sum // return语句若放到while循环中,则执行一次循环就会结束该方法。所以应该放到与while语句平级位置待while循环结束之后,返回总和
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
举报