我的问题与 python 中的求和函数有关。
所以我的代码是
def black_jack(a, b):
if sum(a, b) > 21:
return 0
else:
return sum(a, b)
print black_jack(10, 5)
我读到一个错误:
'int' object is not iterable
有人可以解释为什么会发生这种情况以及如何解决吗?
请您参考如下方法:
sum(iterable[, start])
Sums start and the items of an iterable from left to right and returns the total. start defaults to
0. The iterable‘s items are normally numbers, and the start value is not allowed to be a string.
所以你必须传递一个可迭代的参数,而不是一个整数!
sum((a, b)) 应该可以正常工作。
这是一个函数,当你有许多值存储在一个列表中时(例如),如果你想对两个值求和,你应该简单地使用 a + b。
