所以我创建了一个项目“公牛和奶牛”。其中,计算机随机生成一个四位数字。然后用户必须猜测一个四位数的数字。如果数字是计算机中的4位数字,那么你得到一头公牛,如果不是,那么你得到一头奶牛。程序运行,直到用户获得所有四位数字
我以为它在工作,直到我把一个数字的四个放进去,它还是接受了。我试着添加一个if语句,但是现在它不计算任何正确的数字
import random
number = random.choice(range(1000, 9999))
number = str(number)
print(number)
flag = True
while flag:
cow = 0
bull = 0
user = input('Number ')
user = str(user)
for item in user:
x = number.count(item)
y = int(item)
if y <= x:
bull += 1
else:
cow += 1
print(f"You have {bull} bulls, and {cow} cows!")
if bull == 4:
print(f'The computers number is {number}')
flag = False
else:
print("Guess again")
我一直在尝试各种不同的方法来解决我的问题,但我就是解决不了:(
复制
number
。然后,当用户猜对一个数字时,将其从该副本中删除。这样,如果他们猜测数字的次数超过数字中出现的次数,那么额外的猜测就不会被复制这个副本应该是一个列表,这样您就可以使用它的
remove()
方法根据之前的建议和我个人的想法,我做了一些改变。进行一点错误检查,以确保此人输入了一个数字,并记录此人的猜测,以便多次猜测同一个数字不会对他们不利
从测试运行中:
相关问题 更多 >
编程相关推荐