int 不是可迭代的 python

尤里

我正在尝试创建 Cows and Bulls 游戏,但得到了一个TypeError: 'int' object is not iterable. 这是我的代码:

import random

print('You are playing Cows and Bulls game !')

number = random.randint(1000,2000)
number = list(number)
print(number)
user_number = input('Guess a number : \n')
global cows, bulls
cows = 0
bulls = 0
user_number = list(user_number)
print(user_number)

def game():
 if user_number[0] in number:
    cows += 1
 elif user_number[1] in number:
    cows += 1
 elif user_number[2] in number:
    cows += 1
 elif user_number[3] in number:
    cows += 1
 elif user_number not in number:
    bulls += 1

game()

当我启动程序时,它给了我:

Traceback (most recent call last):
    File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\Cows and
        number = list(number)
TypeError: 'int' object is not iterable

我不知道该怎么办,请帮忙,我刚刚开始,所以可能会有一些愚蠢的错误,如果是这样,请务必纠正我:) 我想它想让我把它变成一个字符串还是什么?

内科

您已尝试将 anint转换为listints 不可迭代,这是list()函数正常工作的要求您只需更改number = list(number)即可使您的代码正常工作number = list(str(number)),但也number = str(number)能正常工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeError:'int'对象不是可迭代的Python?

int对象不是可迭代的python

MysqlDB调用TypeError:'int'对象不是可迭代的python

Python线程错误-必须是可迭代的,而不是int

'int'对象不是可迭代的python暴力密码

TypeError:“ int”对象不是可迭代的Python读取文件

*之后的参数必须是可迭代的,而不是int

获取'int'对象不是可迭代的错误

Python列表到csv会引发错误:可迭代的预期值,而不是numpy.int64

Python'float'对象不是可迭代的错误

“int”对象不是可迭代的错误?怎么修?

*之后的_reverse_with_prefix()参数必须是可迭代的,而不是int

*之后的add()参数必须是可迭代的,而不是int Pygame

类型错误:* 后的 start() 参数必须是可迭代的,而不是 int

类型错误:Int对象不是可迭代错误[For循环]

在 KERAS 中检索“int”对象不是可迭代错误的问题

Python:获取类型为int的参数不是可重复的错误

Python中的“ NoneType”对象不是可迭代错误

TypeError:'float'对象在python 3中不是可迭代的错误

*之后的/ polls / 1 / vote / _reverse_with_prefix()参数处的Django TypeError必须是可迭代的,而不是int

'int'对象是不是可迭代的并且两个代码之间的区别?

“可迭代”在Python中到底是什么意思?为什么我的实现__getitem __()的对象不是可迭代的?

Python:使类可迭代

Python可迭代协议

Python:复杂的可迭代

Python for 循环不是迭代,

python-add()不会将类的实例添加到组中,因为它不是可迭代的,但是它是在迭代的

TypeError:当我使用.join时,“类型”对象不是可迭代的python

TypeError:'int'对象不是可迭代的序列化器Django序列化器一对多