简体   繁体   中英

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. 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 . It tells if the computer is allowed to lie, it's True if the last thing he told was the truth.
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 .
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.
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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