Python - TypeError:'int'对象不可迭代 [英] Python - TypeError: 'int' object is not iterable

查看:52
本文介绍了Python - TypeError:'int'对象不可迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

导入数学打印(嘿,让我们解决任务 4 :)")number1 = input("你想看多少位数字?")number2 = input("你希望数字加起来是多少?")如果数字 1 == 1:牛 = 范围(0,10)elif 编号 1 == 2:牛 = 范围(10,100)elif 编号 1 == 3:牛 = 范围(100,1000)elif 编号 1 == 4:牛 = 范围(1000,10000)elif 编号 1 == 5:牛 = 范围(10000,100000)elif 编号 1 == 6:牛 = 范围(100000,1000000)elif 编号 1 == 7:牛 = 范围(1000000,10000000)elif number1 == 8:牛 = 范围(10000000,100000000)elif 编号 1 == 9:牛 = 范围(100000000,1000000000)elif number1 == 10:牛 = 范围(1000000000,10000000000)number3 = 牛[-1] + 1n = 0当 n <编号 3:number4 = list(cow[n])n += 1

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

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

TypeError: 'int' 对象不可迭代

我做错了什么?

解决方案

您的问题在于这一行:

number4 = list(cow[n])

它尝试获取返回一个整数的 cow[n] 并将其变成一个列表.这不起作用,如下所示:

<预><代码>>>>一 = 1>>>清单(一)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:int"对象不可迭代>>>

也许你想把 cow[n] inside 一个列表:

number4 = [cow[n]]

请看下面的演示:

<预><代码>>>>一 = 1>>>[一种][1]>>>

<小时>

另外,我想解决两件事:

  1. 您的 while 语句末尾缺少 :.
  2. 像这样使用 input 被认为是非常危险的,因为它会将其输入评估为真正的 Python 代码.这里最好使用 raw_input 然后转换输入转换为带有 int 的整数.

<小时>

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

<预><代码>>>>一 = 137>>>a = str(a)>>># 这种方式比较常用和首选>>>sum(int(x) for x in a)11>>># 但这也有效>>>总和(地图(int,a))11>>>

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 137 was 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).

However, I keep getting this error message:

TypeError: 'int' object is not iterable

What am I doing wrong?

解决方案

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:

>>> 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] inside a list:

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 input like that, since it evaluates its input as real Python code. It would be better here to use raw_input and then convert the input to an integer with 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:

>>> 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
>>>

这篇关于Python - TypeError:'int'对象不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆