简体   繁体   English

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

[英]How to make guessing the number game(high, low hints) with cheating allowed (Ulam's Game) in python?

I made a game of guessing the number where computer choose a secret number between 1 to 100 and i try to guess that number.我做了一个猜数字的游戏,计算机在 1 到 100 之间选择一个秘密数字,然后我试着猜这个数字。 On each guess, computer gives hint that whether it is lower or higher than his secret number by saying "too low" or "too high".在每次猜测中,计算机都会通过说“太低”或“太高”来暗示它是低于还是高于他的秘密数字。 But i want that sometimes computer will cheat ( for example in place of saying "too low" it will say "too high").但我希望有时计算机会作弊(例如,代替说“太低”,它会说“太高”)。 But whenever he tells a lie, in very next guess he must have to tell the truth.但每当他说谎时,在下一个猜测中,他必须说实话。 He must not to be allowed to lie in two consecutive guesses.不能让他连续两次猜测都说谎。 Though he can lie alternatively.虽然他可以说谎。 He may tell the truth without any limitation, consecutively and anytime.他可以无限制地连续、随时地讲真话。 In below code, computer randomly say "too low" or "too high".在下面的代码中,计算机随机说“太低”或“太高”。 By doing so he is sometimes lying consecutively more than once.通过这样做,他有时会连续撒谎不止一次。 Please fix the code so that it doesn't lie in two consecutive guesses.请修复代码,使其不会出现在两个连续的猜测中。

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))
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))

However, you should have tried a bit more to solve it.但是,您应该尝试更多来解决它。

Here is how I solved the problem.这是我解决问题的方法。
We define a variable can_lie .我们定义了一个变量can_lie It tells if the computer is allowed to lie, it's True if the last thing he told was the truth.它告诉计算机是否允许撒谎,如果他说的最后一件事是True ,那就是真的。
Then if we are allowed to lie, and only one time over two, we lie.然后,如果我们被允许撒谎,而且只有一次超过两次,我们就会撒谎。
After this we are not allowed to do it for the next turn, so can_lie become False .在此之后,我们不允许在下一回合这样做,所以can_lie变为False
Then we say the opposite of what we should have said, because we are lying.然后我们说与我们应该说的相反的话,因为我们在撒谎。
If we are telling the truth, then can_lie become True , so we are allowed to tell whatever we want the next turn.如果我们说的是真话,那么can_lie变为True ,因此我们可以在下一回合说出我们想要的任何内容。
Then we tell what we are supposed to say.然后我们说出我们应该说的话。


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))

This code lies randomly, but never twice in a row.这段代码是随机的,但不会连续出现两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM