整数总和 - “int"对象不可迭代 [英] Sum of Integers - 'int' object is not iterable
问题描述
我的问题与 python 中的 sum 函数有关.
My question is related to the sum function in 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
有人可以解释为什么会发生这种情况以及如何解决吗?
Can someone explain why this happens and how to fix it?
推荐答案
看文档:
sum(iterable[, start])
从左到右对开始和可迭代项的总和并返回总数.start 默认为 0
.可迭代的项目是通常是数字,起始值不能是字符串.
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.
所以你必须传递一个 iterable 作为参数,而不是一个 int!
So you have to pass an iterable as argument, not an int!
sum((a, b))
应该可以正常工作.
这是一个函数,当你有很多值存储在一个列表中时(例如),如果你想对两个值求和,你应该简单地使用 a + b
.
This is a function which is intended to be used when you have many values stored in a list (for example), if you want to sum two values, you should simply use a + b
.
这篇关于整数总和 - “int"对象不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!