def calculate_total_sum(arr):
# Your code here
sum=0
for i in range(len(arr)):
for j in range(len(arr[i])):
sum=sum+arr[i][j]
print(sum)
list1=[int(x) for x in input().split()]
calculate_total_sum(list1)
运行代码时出现错误 -
unsupported operand type(s) for +: 'int' and 'str'
我们可以在这里使用dfs:
def calculate_total_sum(arr):
def dfs(arr, total):
if arr is None:
return total
if type(arr) == int:
return total + arr
for el in arr:
total = dfs(el, total)
return total
total = 0
return dfs(arr, total)
list1 = [3, 3, [[3, 3, [3, [3, [3, [3, [3, [3, [3, 3, 3]]]]]]], 3]]]
print(calculate_total_sum(list1))