Python - TypeError:'int'对象不可迭代

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19523563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:57:56  来源:igfitidea点击:

Python - TypeError: 'int' object is not iterable

pythonlistloopsiterable

提问by

Here's my code:

这是我的代码:

import math

print "Hey, lets solve Task 4 :)"

number1 = input ("How many digits do you want to look at? ")
number2 = input ("What would you like the digits to add up to? ")

if number1 == 1:
    cow = range(0,10)
elif number1 == 2:
    cow = range(10,100)
elif number1 == 3:
    cow = range(100,1000)
elif number1 == 4:
    cow = range(1000,10000)
elif number1 == 5:
    cow = range(10000,100000)
elif number1 == 6:
    cow = range(100000,1000000)
elif number1 == 7:
    cow = range(1000000,10000000)
elif number1 == 8:
    cow = range(10000000,100000000)
elif number1 == 9:
    cow = range(100000000,1000000000)
elif number1 == 10:
    cow = range(1000000000,10000000000)

number3 = cow[-1] + 1

n = 0
while n < number3:
    number4 = list(cow[n])
    n += 1

I am looking to make a loop so that for each element in the list, it will get broken down into each of it's characters. For example, say the number 137was in the list then it would be turned into [1,3,7]. Then I want to add these numbers together (I haven't started that bit yet but I have some idea of how to do it).

我正在寻找一个循环,以便对于列表中的每个元素,它将被分解为每个字符。例如,假设数字137在列表中,那么它将变成[1,3,7]. 然后我想将这些数字加在一起(我还没有开始那一点,但我知道如何去做)。

However, I keep getting the error message

但是,我不断收到错误消息

TypeError: 'int' object is not iterable

when I try and run this.

当我尝试运行它时。

What am I doing wrong?

我究竟做错了什么?

采纳答案by

Your problem is with this line:

您的问题出在这一行:

number4 = list(cow[n])

It tries to take cow[n], which returns an integer, and make it a list. This doesn't work, as demonstrated below:

它尝试采用cow[n]返回整数的 ,并将其设为列表。这不起作用,如下所示:

>>> a = 1
>>> list(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>>

Perhaps you meant to put cow[n]insidea list:

也许你的意思是把cow[n]里面的列表:

number4 = [cow[n]]

See a demonstration below:

请参阅下面的演示:

>>> a = 1
>>> [a]
[1]
>>>


Also, I wanted to address two things:

另外,我想解决两件事:

  1. Your while-statement is missing a :at the end.
  2. It is considered very dangerous to use inputlike that, since it evaluates its input as real Python code. It would be better here to use raw_inputand then convert the input to an integer with int.
  1. 你的 while 语句:最后缺少一个。
  2. input那样使用被认为是非常危险的,因为它会将其输入评估为真正的 Python 代码。最好在这里使用raw_input然后将输入转换为整数int


To split up the digits and then add them like you want, I would first make the number a string. Then, since strings are iterable, you can use sum:

要拆分数字然后根据需要添加它们,我首先将数字设为字符串。然后,由于字符串是可迭代的,您可以使用sum

>>> a = 137
>>> a = str(a)
>>> # This way is more common and preferred
>>> sum(int(x) for x in a)
11
>>> # But this also works
>>> sum(map(int, a))
11
>>>

回答by grepit

This is very simple you are trying to convert an integer to a list object !!! of course it will fail and it should ...

这很简单,您尝试将整数转换为列表对象!!!当然它会失败,它应该......

To demonstrate/prove this to you by using the example you provided ...just use type function for each case as below and the results will speak for itself !

为了通过使用您提供的示例向您演示/证明这一点......只需为每个案例使用类型函数,如下所示,结果将不言自明!

>>> type(cow)
<class 'range'>
>>> 
>>> type(cow[0])
<class 'int'>
>>> 
>>> type(0)
<class 'int'>
>>> 
>>> >>> list(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>>