請問這道題的邏輯錯在哪里?
def?square_of_sum(L): ????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])
def?square_of_sum(L): ????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])
2016-10-13
舉報
2016-10-30
sum還要賦初值 sum=0
2016-10-13
sum=sum(x*x) ? 換成 sum + x *x
2016-10-13
sum是一個系統的內置函數,你可以在shell里用help(sum)調用看下用法,(如下):
Help on built-in function sum in module __builtin__:
sum(...)
? ? sum(sequence[, start]) -> value
? ??
? ? Return the sum of a sequence of numbers (NOT strings) plus the value
? ? of parameter 'start' (which defaults to 0). ?When the sequence is
? ? empty, return start.
2016-10-13
sum的參數是一個list。sum(x*x)參數不對。