在python中,如何使用允许作弊(Ulam的游戏)来猜数字游戏(高、低提示)?

2024-05-19 02:09:26 发布

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

我做了一个猜数字的游戏,电脑选择一个介于1到100之间的秘密数字,我试着猜那个数字。在每一次猜测中,计算机都会通过说“太低”或“太高”来提示是否低于或高于他的秘密号码。但我希望有时计算机会作弊(例如,在说“太低”时,它会说“太高”)。但无论何时他说谎,在接下来的猜测中,他一定要说实话。决不能允许他连续两次猜谎。虽然他也可以撒谎。他可以无限制地、连续地、随时讲真话。在下面的代码中,计算机随机地说“太低”或“太高”。他这样做有时是连续不止一次地撒谎。请修复代码,使其不会出现连续两次猜测

import random

myName = input('What is your name?~ ')
print('Well, ' + myName + '! I am thinking of a number from 1 to 100')
number = random.randint(1, 100)
guessesTaken = 0
result = ['low', 'high']

while guessesTaken < 20:
    guess = int(input(str(guessesTaken + 1) + '-Take a guess!~ '))
    guessesTaken += 1
    if guess < number:
        print('Your guess is too ' + random.choice(result) + '!')
    if guess > number:
        print('Your guess is too ' + random.choice(result) + '!')
    if guess == number:
        break

if guess == number:
   print('Good job, ' + myName + '! You guess my number in ' + str(guessesTaken) + ' guesses')
else:
    print('Nope! The number I was thinking of was ' + str(number))

Tags: 代码numberinputifis计算机数字random
2条回答
import random
myName = input('What is your name?~ ')
print('Well, ' + myName + '! I am thinking of a number from 1 to 100')
number = random.randint(1, 100)
guessesTaken = 0
result = ['low', 'high']
previous_prediction = True

while guessesTaken < 20:
    guess = int(input(str(guessesTaken + 1) + '-Take a guess!~ '))
    guessesTaken += 1
    if guess == number:
        break
    if previous_prediction: 
        low_high = random.choice(result)
    else: # Make random choice only if told truth in previous choice
        low_high = 'low' if guess < number else 'high'

    print('Your guess is too ' + low_high + '!')
    #If there is a lie between actual result and told result, mark the prediction as False, such that next time, only truth is provided
    if not (( (guess < number) and low_high=='low') or ( (guess > number) and low_high=='high')):
        previous_prediction = False
    else:
        previous_prediction = True

if guess == number:
    print('Good job, ' + myName + '! You guess my number in ' + str(guessesTaken) + ' guesses')
else:
    print('Nope! The number I was thinking of was ' + str(number))

但是,您应该尝试更多的方法来解决它

下面是我如何解决这个问题的。
我们定义一个变量can_lie。它告诉你电脑是否被允许撒谎,如果他最后说的是事实,那就是True 然后如果我们被允许撒谎,并且只有一次超过两次,我们就撒谎了。
在此之后,我们不允许在下一轮中这样做,因此can_lie变成False
然后我们会说与我们应该说的相反的话,因为我们在撒谎。
如果我们说的是真话,那么can_lie就变成了True,所以我们可以在下一轮中说出我们想说的任何话。
然后我们说出我们应该说的话


import random

myName = input('What is your name?~ ')
print('Well, ' + myName + '! I am thinking of a number from 1 to 100')
number = random.randint(1, 100)
guessesTaken = 0
result = ['low', 'high']
can_lie = True
while guessesTaken < 20:
    guess = int(input(str(guessesTaken + 1) + '-Take a guess!~ '))
    guessesTaken += 1
    if can_lie and random.choice((True, False)):
        #Here we lie
        can_lie = False
        if guess > number:
            print('Your guess is too low !')
        if guess < number:
            print('Your guess is too high !')
    else:
        can_lie = True
        if guess < number:
            print('Your guess is too low !')
        if guess > number:
            print('Your guess is too high !')
    if guess == number:
        break

if guess == number:
   print('Good job, ' + myName + '! You guess my number in ' + str(guessesTaken) + ' guesses')
else:
    print('Nope! The number I was thinking of was ' + str(number))

此代码是随机的,但不会连续出现两次

相关问题 更多 >

    热门问题