如何将用户输入作为按键,检测它,并使程序在Python中相应地运行?

问题描述 投票:0回答:1

我目前正在学习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!")

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

python input
1个回答
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!")
© www.soinside.com 2019 - 2024. All rights reserved.