0

I am currently learning python and decided to create a basic game HiLo. I've managed to code the basics but now I'd like to take it further and get the answers as a key press. At first I created a list with available answers as "h, l, etc." but now I'd the user to press UP ARROW KEY for high and DOWN ARROW KEY for low and check the answer is correct.

I've tried with msvcrt, keyboard and pyautogui modules but I could not seem to make it work.

import random
import pygame
from msvcrt import getch


name = input("Please enter your name: ")
print("Welcome {}".format(str(name)))

print(
    "Okay {}. We will now play a game called high or low. In order to play this game you have to be 18 or older".format(
        str(name)))

age = int(input("Please enter your age: "))
print(age)

if age < 18:
    print("You are not old enough the play.")
    exit
else:
    print("OK! You are {}. You can play".format(str(age)))

x = random.randint(1, 9)
y = random.randint(1, 9)

print("Your first number is {}.".format(str(x)))

counter = 0
truecounter = 0
falsecounter = 0

while  counter <= 10: 
    print("Press UP for high and DOWN for low.")
    getch()
    if  and y > x: #If UP Arrow is pressed and Y is greater than X 
        truecounter += 1
        print("Your answer is correct!. The other number was " + str(y))
        x = random.randint(1, 9)
        y = random.randint(1, 9)
        print("Let's see if you can do it again. The number is")
        print(x)

    elif and y < x: #If DOWN Arrow is pressed and Y is smaller than X 
        truecounter += 1
        print("Your answer is correct!. The other number was " + str(y))
        x = random.randint(1, 9)
        y = random.randint(1, 9)
        print("Let's see if you can do it again. The number is")
        print(x)

    elif  and y > x: #If DOWN ARROW is pressed and Y is greater than X
        falsecounter += 1
        print("Ooops! You guessed wrong. The number was " + str(y))
        x = random.randint(1, 9)
        y = random.randint(1, 9)
        print("Let's see if you can do it again. The number is")
        print(x)
    elif  and y < x: #If UP ARROW is pressend and Y is smaller than X
        falsecounter += 1
        print("Ooops! You guessed wrong. The number was " + str(y))
        x = random.randint(1, 9)
        y = random.randint(1, 9)
        print("Let's see if you can do it again. The number is")
        print(x)

    counter += 1

print("Congrats! You got " + str(truecounter) + " out of 10!")

I expect that according to input of the user ( UP or DOWN arrow key) the code will check the numbers and add a point to true or false counter.

  • First google result. https://www.google.ca/search?rlz=1C1GCEV_enCA826CA826&ei=YclRXKmeIcm4jwTF6ZaIBg&q=python+get+keystroke&oq=python+get+keystroke&gs_l=psy-ab.3..0.7709.8444..8532...0.0..0.216.624.0j3j1......0....1..gws-wiz.......0i71j0i22i30.6RT_7ngj0Kw – KuboMD Jan 30 '19 at 15:57
  • I think it answers your question better than I could: https://stackoverflow.com/a/22398481/10913576 – Víctor Hugo Jan 30 '19 at 16:25

1 Answers1

0

Here this works. What are the problem with the keyboard libary?

import random
import keyboard


name = input("Please enter your name: ")
print("Welcome {}".format(str(name)))

print(
    "Okay {}. We will now play a game called high or low. In order to play this game you have to be 18 or older".format(
    str(name)))

age = int(input("Please enter your age: "))
print(age)

if age < 18:
    print("You are not old enough the play.")
    exit
else:
    print("OK! You are {}. You can play".format(str(age)))

x = random.randint(1, 9)
y = random.randint(1, 9)

print("Your first number is {}.".format(str(x)))

counter = 0
truecounter = 0
falsecounter = 0
def check(key):
    global UP_PRESS,DOWN_PRESS,run
    if key.__dict__["event_type"] == "down":
        DOWN_PRESS = True
        run = True
    elif key.__dict__["event_type"] == "up":
        UP_PRESS = True
        run = True

    else:
        raise KeyError("Not the right Key")

while  counter <= 10: 
    print("Press UP for high and DOWN for low.")
    UP_PRESS = False
    DOWN_PRESS = False
    run = False
    while not run:
        keyboard.unhook_all()
        try:
            event = keyboard.hook(check)
        except: 
            print("ERROR: Press Arrow Up or Arrow Down")
    print("\n")
    if UP_PRESS and y > x: #If UP Arrow is pressed and Y is greater than X 
            truecounter += 1
            print("Your answer is correct!. The other number was " + str(y))
            x = random.randint(1, 9)
            y = random.randint(1, 9)
            print("Let's see if you can do it again. The number is")
            print(x)

    elif DOWN_PRESS and y < x: #If DOWN Arrow is pressed and Y is smaller than X 
           truecounter += 1
           print("Your answer is correct!. The other number was " + str(y))
           x = random.randint(1, 9)
           y = random.randint(1, 9)
           print("Let's see if you can do it again. The number is")
           print(x)

    else:
          falsecounter += 1
          print("Ooops! You guessed wrong. The number was " + str(y))
          x = random.randint(1, 9)
          y = random.randint(1, 9)
          print("Let's see if you can do it again. The number is")
          print(x)

    counter += 1

print("Congrats! You got " + str(truecounter) + " out of 10!")