Number guessing game (3+- guessed result)

Posted by Nick Waring on Stack Overflow See other posts from Stack Overflow or by Nick Waring
Published on 2013-10-28T15:49:34Z Indexed on 2013/10/28 15:53 UTC
Read the original article Hit count: 117

Filed under:

I've been assigned a task to create a game that generates 4 digits and the user has to guess the digits one at a time to get the correct result. If the number is correct a Y is displayed and if not, a N. This was easy, now the next step was to implement another two responses. If the answer is too high, a H is displayed and too low, an N. Again, was easy - now the third is to use the same design as game 2 but if the number is 3 higher than a H is displayed and same if it's 3 lower than a L is displayed - otherwise an X is displayed. I can't figure out how to do this.

Here's my test code for game 2 for just one of the digits - any help is appreciated. (5 was used just for a test.)

def guess():
    x = 5
    g= int(input("Guess the number: "))
    if g == x:
        print("Y")
    elif g < x:
        print("L")
    else:
        print("H")

© Stack Overflow or respective owner

Related posts about python