所以我创建了一个名为“公牛和母牛”的程序。在它中,计算机随机生成一个四位数。然后用户必须猜测一个四位数的数字。如果数字在计算机的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")我已经尝试了各种不同的方法来解决我的问题,但我就是解决不了:
发布于 2021-04-17 03:21:14
复制一份number。然后,当用户猜对一个数字时,将其从该副本中删除。这样,如果他们猜测数字的次数比它在数字中出现的次数多,那么额外的猜测就不会被复制。
这个副本应该是一个list,这样您就可以使用它的remove()方法。
while True:
cow = 0
bull = 0
user = input('Number ')
num_list = list(number)
for item in user:
if item in num_list:
bull += 1
num_list.remove(item)
else:
cow += 1
print(f"You have {bull} bulls, and {cow} cows!")
if bull == 4:
print(f'The computer's number is {number}')
break
else:
print("Guess again")发布于 2021-04-17 03:47:42
根据之前的建议和我个人的想法,我做了一些修改。进行一点错误检查,以确保用户输入了数字,并记录用户的猜测,这样多次猜测相同的数字将不会对他们不利。
import random
orig_number = random.choice(range(1000, 9999))
copy_number = str(orig_number)
cows = 0
bulls = 0
guesses = []
def convert_to_int(data):
if data is None:
return False
try:
id = int(data)
except ValueError:
return False
else:
return True
while True:
guess = input("Guess a number > ")
if not convert_to_int(str(guess)[0]):
print("That was not a number")
continue
if str(guess)[0] in guesses:
print("You already guessed that number")
continue
elif str(guess)[0] in copy_number:
print("Good guess, you get a bull")
bulls += 1
copy_number = copy_number.replace(str(guess)[0],'')
else:
print("Too bad, you get a cow")
cows += 1
guesses.append(str(guess)[0])
print("You have {} cows and {} bulls".format(cows,bulls))
if len(copy_number) == 0: break
print("Congratulations! You guessed all the numbers. The original number was {}".format(orig_number))从测试运行中:
Guess a number > a
That was not a number
Guess a number > -
That was not a number
Guess a number > 9
Too bad, you get a cow
You have 1 cows and 0 bulls
Guess a number > 1
Too bad, you get a cow
You have 2 cows and 0 bulls
Guess a number > 4
Too bad, you get a cow
You have 3 cows and 0 bulls
Guess a number > 3
Too bad, you get a cow
You have 4 cows and 0 bulls
Guess a number > 6
Too bad, you get a cow
You have 5 cows and 0 bulls
Guess a number > 5
Good guess, you get a bull
You have 5 cows and 1 bulls
Guess a number > 7
Too bad, you get a cow
You have 6 cows and 1 bulls
Guess a number > 8
Too bad, you get a cow
You have 7 cows and 1 bulls
Guess a number > 9
You already guessed that number
Guess a number > 0
Good guess, you get a bull
You have 7 cows and 2 bulls
Guess a number > 1
You already guessed that number
Guess a number > 2
Good guess, you get a bull
You have 7 cows and 3 bulls
Congratulations! You guessed all the numbers. The original number was 2055https://stackoverflow.com/questions/67130674
复制相似问题