Python一位数字输入

时间:2019-09-30 01:30:56

标签: python python-3.x

我正在尝试为我的办公室构建一个蜂鸣器系统。我们有用作键盘的旧桨,并发出1-9。我正在寻找它放出来的数字并在python中声明一个名称。我已经建立了它,但是我终生无法找到如何为尝试time.sleep的蜂鸣器创建锁定,但是领事馆仍在使用键盘输入。同样,我也找不到如何在输入字段中输入一个数字后立即按下该键而无需按Enter的方法。任何帮助或指向正确方向都是非常好的。仅供参考,这是我第一次使用Python。

while(count != '0'):

  buzz = input("\n Buzzers Ready: ")

  if buzz == '1' and count > '0':
    print(ply1)
    time.sleep(5)
  elif buzz == '2' and count > '1':
    print(ply2)
    time.sleep(5)
  elif buzz == '3' and count > '2':
    print(ply3)
    time.sleep(5)
  elif buzz == '4' and count > '3':
    print(ply4)
    time.sleep(5)
  elif buzz == '5' and count > '4':
    print(ply5)
    time.sleep(5)
  elif buzz == '6' and count > '5':
    print(ply6)
    time.sleep(5)
  elif buzz == '7' and count > '6':
    print(ply7)
    time.sleep(5)
  elif buzz == '8' and count > '7':
    print(ply8)
    time.sleep(5)
  elif buzz == '9' and count > '8':
    print(ply9)
    time.sleep(5)
  elif buzz == 'z':
    break

  else:
    print("Non-Vaild Player.")

2 个答案:

答案 0 :(得分:1)

放置time.sleep(5)而不是break,它将退出循环并不再接受任何按键操作。

if buzz == '1' and count > '0':
    print(ply1)
    time.sleep(5)  <<--- put 'break' here instead everywhere

答案 1 :(得分:0)

  

在输入字段中只有一位数字后,我找不到如何进行操作,而无需按Enter即可完成按键操作。

有两种不同的解决方案。看到以下问题:raw_input in python without pressing enter

过去,我曾经使用过pynput。 (它不需要窗口/控制台的焦点即可读取按键。这可能对您来说不是问题。)

通过在控制台中输入pip install pynput(在Windows上为cmd.exe)进行安装。

看看这个。它将读取按键,直到按下Esc键为止;如果按下1-9,则会打印播放器的名称。直到经过5秒(不使用time.wait()),它才会再次打印玩家的名字。

import time
from pynput import keyboard

LOCKOUT_TIME = 5.000 # Seconds
time_last_pressed = 0 # Or time.time() - LOCKOUT_TIME

names = ("Skinny Pete (player 1)",
         "Old Joe (player 2)",
         "Badger (player 3)",
         "Emilio (player 4)",
         "Bogdan (player 5)", 
         "Hector (player 6)",
         "Gale (player 7)",
         "Mike (player 8)",
         "Gus (player 9)")

def on_press(key):
    # Without this global statement, assigning to time_last_pressed will create 
    # a new local variable, which won't exist once the function ends.
    global time_last_pressed

    # Make sure enough time has passed before continuing
    if time.time() - time_last_pressed < LOCKOUT_TIME:
        return

    # The "key" parameter will only have the "char" attribute if a normal 
    # alphanumeric key was pressed. If another key is pressed, such as shift, 
    # an AttributeError exception will be raised which we will catch.
    # key.char will be a string, e.g. "1" if the "1" key is pressed, "a" if the 
    # "a" key is pressed, etc. We want it as an int, so we can use it to index 
    # into the names tuple. int will raise a ValueError if you pass in a string 
    # which cannot be parsed as an int - every time any letter key is pressed.
    try:
        number = int(key.char)
    except (ValueError, AttributeError):
        return

    # Make sure the number is between 1 and 9 before continuing
    # if number < 1 or number > 9: # This would work, too
    if 9 > number < 1:
        return

    # Indexing for lists/tuples is 0-based, so we want a key press of "1" to 
    # actually access element 0, not element 1.
    print(names[number-1])

    # Update this only now - once a valid key press has been processed
    time_last_pressed = time.time() 

def on_release(key):
    if key == keyboard.Key.esc:
        return False # Stop listener

# Collect events until released
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()

使用正确的软件包/模块,您可以在屏幕上显示播放器的名称(而不是将其打印到控制台),并在播放器按下其按钮时播放蜂鸣声。