求解答,為什么輸出的答案不對?
def square_of_sum(x):
? ? result = 0
? ? for M in x :
? ? ? ? y=[]
? ? ? ? y.append(M*M)
? ? ? ??
? ? result=sum(y)
? ? return result
print(square_of_sum([1, 2, 3, 4, 5]))
print(square_of_sum([-5, 0, 5, 15, 25]))
def square_of_sum(x):
? ? result = 0
? ? for M in x :
? ? ? ? y=[]
? ? ? ? y.append(M*M)
? ? ? ??
? ? result=sum(y)
? ? return result
print(square_of_sum([1, 2, 3, 4, 5]))
print(square_of_sum([-5, 0, 5, 15, 25]))
2023-04-22
舉報
2023-04-23
def square_of_sum(x):
? ?result = 0
? ?y = []
? ?
? ?for M in x:
? ? ? ?y.append(M * M)
? ?result = sum(y)
? ?return result
print(square_of_sum([1, 2, 3, 4, 5]))
print(square_of_sum([-5, 0, 5, 15, 25]))
你把創建空列表放在循環外就正確了,如果循環一次就創建一個新的列表,那后面的列表會覆蓋前面的列表,第一個列表最后一個元素是5,5的平方是25,你的代碼運行結果就是這個。正確的答案是55
翻譯
搜索
復制
2023-04-22