對於問答題必須使用嵌套列表來查找每行的總和,而無需使用內置的總和()和列表的列表行的,不使用內置的總和()
我的代碼(1)
def row_sums(square):
""" Takes a nested list and returns the sum of the row"""
row_1 = square[0]
for sum_1 in row_1:
for sum_r_1 in sum_1:
sum_row_1 = 0
sum_r_1 += sum_row_1
return sum_r_1
的問題,這是我得到的int對象不是可迭代
或
我的代碼(2)
def row_sums(square):
""" Takes a nested list and returns the sum of the row"""
row_1 = square[0]
for sum_1 in row_1:
sum_r_1 = 0
sum_r_1 += sum_1
return sum_r_1
我只得到第1行中列表的第一項在這種情況下是2號
從測驗服務器
square = [
[2, 7, 6],
[9, 5, 1],
[4, 3, 8]
]
print(row_sums(square))
預期的結果 [15,15輸入,15
我知道我在試圖讓大氣壓只有第一排,但一旦我有我可以輕鬆地做其他行
我可能會寫一些愚蠢像'高清totally_not_the_built_in_sum(項目)寫法:return eval( '+' 加入(STR(它),它在項目))' –