菜鳥問題,答案中的幾個定義
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])
上面是標準答案,想問問def square_of_sum(L): 中的L 是什么?
?sum = sum + x * x 計算機怎么知道x是什么?
2018-09-28
L在這個里面是指傳入的參數(list),
x是局部變量,每次循環依次取L中的一項元素
2018-09-28
L是函數square_of_sum的形參,是list,for x in L循環中,計算機會依次讀取L中的元素,并賦值給x。