如何确保int在while循环中只扣除一次(python3)

2024-09-30 05:32:39 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我创建了一个项目“公牛和奶牛”。其中,计算机随机生成一个四位数字。然后用户必须猜测一个四位数的数字。如果数字是计算机中的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")

我一直在尝试各种不同的方法来解决我的问题,但我就是解决不了:(


Tags: 用户numberif计算机数字randomitemflag
2条回答

复制number。然后,当用户猜对一个数字时,将其从该副本中删除。这样,如果他们猜测数字的次数超过数字中出现的次数,那么额外的猜测就不会被复制

这个副本应该是一个列表,这样您就可以使用它的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")

根据之前的建议和我个人的想法,我做了一些改变。进行一点错误检查,以确保此人输入了一个数字,并记录此人的猜测,以便多次猜测同一个数字不会对他们不利

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 2055

相关问题 更多 >

    热门问题