int 对象在尝试对数字的数字求和时不可迭代?
我有这段代码:
inp = int(input("Enter a number:"))
for i in inp:
n = n + i;
print (n)
但它抛出一个错误:'int' object is not iterable
我想通过添加每个数字来找出总数,例如 110.1 + 1 + 0 = 2。我该怎么做?
I have this code:
inp = int(input("Enter a number:"))
for i in inp:
n = n + i;
print (n)
but it throws an error: 'int' object is not iterable
I wanted to find out the total by adding each digit, for eg, 110. 1 + 1 + 0 = 2. How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
首先,丢失对 int 的调用 - 您正在将字符串转换为整数,这不是您想要的(您希望将每个字符视为其自己的数字)。更改
为:
现在
inp
是一串数字,您可以逐位循环它。接下来,为
n
分配一些初始值 - 正如您现在的代码所示,您将得到一个NameError
因为您从未初始化它。大概您希望在for
循环之前使用n = 0
。接下来,再次考虑字符和整数之间的区别。现在你有了:
除了不必要的分号(Python 是基于缩进的语法)之外,它还试图将字符 i 与整数 n 相加——这将不会'不工作!因此,this 就变成了
将字符
'7'
转换为整数7
,依此类推。First, lose that call to
int
- you're converting a string of characters to an integer, which isn't what you want (you want to treat each character as its own number). Change:to:
Now that
inp
is a string of digits, you can loop over it, digit by digit.Next, assign some initial value to
n
-- as you code stands right now, you'll get aNameError
since you never initialize it. Presumably you wantn = 0
before thefor
loop.Next, consider the difference between a character and an integer again. You now have:
which, besides the unnecessary semicolon (Python is an indentation-based syntax), is trying to sum the character i to the integer n -- that won't work! So, this becomes
to turn character
'7'
into integer7
, and so forth.也许您正在尝试
这将打印您的输入值(inp)次,要仅打印一次,请执行以下操作:
对于范围内的 i(inp - inp + 1 )
print(i)
我刚刚遇到这个错误,因为我没有使用 range()
maybe you're trying to
This will print your input value (inp) times, to print it only once, follow:
for i in range(inp - inp + 1 )
print(i)
I just had this error because I wasn't using range()
try:
这将迭代字符串表示形式中的字符。一旦你拥有了每个字符,你就可以像使用单独的数字一样使用它。
try:
That will iterate over the characters in the string representation. Once you have each character you can use it like a separate number.
好吧,您想要处理表示数字的字符串,迭代数字,而不是数字本身(这是一个可以以不同方式编写的抽象实体,例如罗马数字中的“CX”或十六进制的“0x6e”(两者都代表 110)或其他)。
因此:
请注意,
n = 0
是必需的(在进入循环之前的某个位置)。您不能获取不存在的变量的值(并且n = n + int(digit)
的右侧采用n
的值) 。如果n
确实存在,它可能包含与您当前需求完全无关的东西,从而导致意外的行为;你需要警惕这一点。该解决方案不会尝试确保用户提供的输入实际上是数字。我将把这个问题留给你思考(提示:你需要的一切都在 Python 教程)。
Well, you want to process the string representing the number, iterating over the digits, not the number itself (which is an abstract entity that could be written differently, like "CX" in Roman numerals or "0x6e" hexadecimal (both for 110) or whatever).
Therefore:
Note that the
n = 0
is required (someplace before entry into the loop). You can't take the value of a variable which doesn't exist (and the right hand side ofn = n + int(digit)
takes the value ofn
). And ifn
does exist at that point, it might hold something completely unrelated to your present needs, leading to unexpected behaviour; you need to guard against that.This solution makes no attempt to ensure that the input provided by the user is actually a number. I'll leave this problem for you to think about (hint: all that you need is there in the Python tutorial).
附注:如果你想获得所有数字的总和,你可以简单地做
Side note: if you want to get the sum of all digits, you can simply do
for .. in
语句期望您使用定义了迭代器的类型。简单 int 类型没有迭代器。for .. in
statements expect you to use a type that has an iterator defined. A simple int type does not have an iterator.正如 ghills 已经提到的那样,
当您循环遍历某些内容时,关键字是“IN”,请始终将其视为某些内容的列表。您不能循环遍历普通整数。因此,它是不可迭代的。
As ghills had already mentioned
When you are looping through something, keyword is "IN", just always think of it as a list of something. You cannot loop through a plain integer. Therefore, it is not iterable.
获取您的输入并确保它是一个字符串,以便它是可迭代的。
然后执行列表理解并将每个值更改回数字。
现在,如果您愿意,您可以对所有数字求和:
或者,如果您确实想在执行时查看输出:
Take your input and make sure it's a string so that it's iterable.
Then perform a list comprehension and change each value back to a number.
Now, you can do the sum of all the numbers if you want:
Or, if you really want to see the output while it's executing:
OP-s问题的一个可能的答案(“我想通过添加每个数字来找出总数,例如,110。1 + 1 + 0 = 2。我该怎么做?”)是使用内置函数divmod()
One possible answer to OP-s question ("I wanted to find out the total by adding each digit, for eg, 110. 1 + 1 + 0 = 2. How do I do that?") is to use built-in function divmod()
你可以尝试改变
对于 inp 中的 i:
进入
对于范围内的 i(1,inp):
迭代不适用于单个 int。相反,您需要为其运行提供一个范围。
You can try to change
for i in inp:
into
for i in range(1,inp):
Iteration doesn't work with a single int. Instead, you need provide a range for it to run.
不要将其设为
int()
,而将其设为range()
将解决此问题。Don't make it a
int()
, but make it arange()
will solve this problem.