Python numbers game reverse

后端 未结 4 1991
我在风中等你
我在风中等你 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: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.

提交回复
热议问题