如何在键盘上获取用户输入作为按键,进行检测,并使程序相应地在Python中起作用?

时间:2019-01-30 15:54:49

标签: python input

我目前正在学习python,并决定创建一个基本游戏HiLo。我已经设法编写了基础知识,但现在我想进一步介绍它,并作为按键来获得答案。首先,我创建了一个列表,其中包含“ h,l等”的可用答案。但是现在我希望用户按向上箭头键为高,按向下箭头键为低,并检查答案是否正确。

我尝试使用msvcrt,键盘和pyautogui模块,但似乎无法使其正常工作。

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!")

我希望代码根据用户的输入(向上或向下箭头键)将检查数字并在真或假计数器上添加一个点。

1 个答案:

答案 0 :(得分:0)

这是可行的。 键盘库有什么问题?

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!")