Python numbers game reverse

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

提交回复
热议问题