Skip to content Skip to sidebar Skip to footer

Python Numbers Game Reverse

So I have to make a 'game' on python were I think of a number and it tries to guess the number. I have to tell it if it's higher or lower than the number it guessed and continue fr

Solution 1:

You can use two different numbers to indicate the lowest and highest guesses.

When the computer guesses a number and its higher actual number, you can make the highest = that number.

Same way when the computer guesses a number and its lower than actual number, you can make the lower = that number.

And each time you take random number between these two lowest and highest number only.

The code would look like -

from random import randrange
deflowHigh():
    l = input ("Please input the low number range.")
    numl = eval(l)
    h = input ("Please input the high number range.")
    numh = eval(h)
    lowest = l
    highest = h
    whileTrue:
        guess = randrange(lowest,highest+1)
        print (guess)
        ask = input("Is this number correct? y for yes or n for no.")
        if ask == 'y':
            print("Yay! I guessed right!")
            breakelse:
            lowOrHigh = input ("Is this number too high or low? h for high, l for low.")
            if lowOrHigh == 'h':
               highest = guess - 1else:
               lowest = guess

Solution 2:

You can save the numbers it guessed in a list and then you check if a new guess is already in the list or not. initialise an empty list like so:

guessed=[]

and then you can append guesses made by your program into the list

guessed.append(guess)

Solution 3:

Demo:

from random import randrange
import time


defguessNumber(min_no, max_no):
    """ Select number from the range. """try:
        return randrange(min_no, max_no)
    except ValueError:
        return min_no


defuserNoInput(msg):
    """ Get Number into from the user. """while1:
        try:
            returnint(raw_input(msg))
        except ValueError:
            print"Enter Only Number string."continuedeffindMe():
    """ 
        1. Get Lower and Upeer Value number from the User.
        2. time sleep to guess number for user in between range.
        3. While infinite loop.
        4. Get guess number from the Computer.
        5. User can check guess number and tell computer that guess number if correct ror not.
        6. If Correct then print msg and break While loop.
        7. If not Correct then 
             Ask Computer will User that guess number is Greate or Lower then Actual number. 
            7.1. If Greater then Set Max limit as guess number.  
            7.2. If Not Greater then Set Min limit as guess number.
            7.3. Continue While loop
    """
    min_no = userNoInput("Please input the low number range:")
    max_no = userNoInput("Please input the high number range:")
    print"Guess any number between %d and %d."%(min_no, max_no)
    max_no += 1
    time.sleep(2)


    whileTrue:
        guess = guessNumber(min_no, max_no)
        print"Computer guess Number:-", guess
        ask = raw_input("Is this number correct? y for Yes or n for No:")
        if ask.lower() == 'y':
            print("Yay! I guessed right!")
            breakelse:
            lowOrHigh = raw_input("Is this number too high or low? h for high, l for low.")
            if lowOrHigh.lower() == 'h':
                #- As guess number is higher then set max number to guess number. 
                max_no = guess
            else:
                #- As guess number is lower then set min number to guess number. 
                min_no = guess

findMe()

Output:

Please input the low numberrange:10Please input the high numberrange:20Guessanynumber between 10 and 20.Computer guess Number:- 14Isthisnumber correct? y forYes or n forNo:n
Isthisnumber too high or low? h for high, l for low.lComputer guess Number:- 19Isthisnumber correct? y forYes or n forNo:n
Isthisnumber too high or low? h for high, l for low.hComputer guess Number:- 17Isthisnumber correct? y forYes or n forNo:n
Isthisnumber too high or low? h for high, l for low.hComputer guess Number:- 16Isthisnumber correct? y forYes or n forNo:n
Isthisnumber too high or low? h for high, l for low.hComputer guess Number:- 15Isthisnumber correct? y forYes or n forNo:y
Yay! I guessed right!

Note:

Python 2.7 : raw_input() method, print is statement.

Python 3.X : input() method, print is function.

Solution 4:

Its very similar to a Binary Search Algorithm

Its just that the usual mid value in such algorithms can be replaced by randrange(Low,High)

I'm not sure if this is a working code but I suggest you do it recursively:

items= range(l,h)
defrandom_search(ask, items, l, h):
    """
    Random search function.
    Assumes 'items' is a sorted list.
    The search range is [low, high)
    """
    ask = input("Is this number correct? y for yes or n for no.")
    lowOrHigh = input ("Is this number too high or low? h for high, l for low.")
    elem = randrange(l,h)

    if ask == 'y':
        return elem
    elif h == l:
        returnFalseelif lowOrHigh == 'h':
        items= range(l,elem)
        return random_search(ask, items, l, elem)
    else:
        items= range(elem, h)
        return random_search(ask, items, elem, h)

Post a Comment for "Python Numbers Game Reverse"