Python numbers game reverse

后端 未结 4 1992
我在风中等你
我在风中等你 2021-01-29 13:07

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 conti

相关标签:
4条回答
  • 2021-01-29 13:20

    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)
    def random_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:
            return False
        elif 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)
    
    0 讨论(0)
  • 2021-01-29 13:33

    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
    def lowHigh():
        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
        while True:
            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!")
                break
            else:
                lowOrHigh = input ("Is this number too high or low? h for high, l for low.")
                if lowOrHigh == 'h':
                   highest = guess - 1
                else:
                   lowest = guess
    
    0 讨论(0)
  • 2021-01-29 13:36

    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)
    
    0 讨论(0)
  • 2021-01-29 13:46

    Demo:

    from random import randrange
    import time
    
    
    def guessNumber(min_no, max_no):
        """ Select number from the range. """
        try:
            return randrange(min_no, max_no)
        except ValueError:
            return min_no
    
    
    def userNoInput(msg):
        """ Get Number into from the user. """
        while 1:
            try:
                return int(raw_input(msg))
            except ValueError:
                print "Enter Only Number string."
                continue
    
    def findMe():
        """ 
            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)
    
    
        while True:
            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!")
                break
            else:
                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 number range:10
    Please input the high number range:20
    Guess any number between 10 and 20.
    Computer guess Number:- 14
    Is this number correct? y for Yes or n for No:n
    Is this number too high or low? h for high, l for low.l
    Computer guess Number:- 19
    Is this number correct? y for Yes or n for No:n
    Is this number too high or low? h for high, l for low.h
    Computer guess Number:- 17
    Is this number correct? y for Yes or n for No:n
    Is this number too high or low? h for high, l for low.h
    Computer guess Number:- 16
    Is this number correct? y for Yes or n for No:n
    Is this number too high or low? h for high, l for low.h
    Computer guess Number:- 15
    Is this number correct? y for Yes or n for No:y
    Yay! I guessed right!
    

    Note:

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

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

    0 讨论(0)
提交回复
热议问题