首页 > 解决方案 > Python:在输入之前清除键盘模块按下的键

问题描述

我有一堂课,就像这样:

import keyboard
import numpy as np

class Something():
    def __init__(self, modulo):
        self.modulo = modulo

    def pick_number(self):
        print ("In this case it seems simple to avoid, but I really need you press q")
        while True:
            keyboard.wait("q")
            n=np.random.randint(0,1000)
            print(n)
            while n%self.modulo!=0:
                n=np.random.randint(0,1000)
                print(n)
            self.number=n
            break

    def add_number(self):
        try:
            n=int(input("Type a number..."))
            self.number+=n
            return self.number
        except:
            print("Please put a correct number...")

然后,我执行 Something() 的新实例:

instance = Something(11)
instance.pick_number()
instance.add_number()

当调用 add_number() 时,我在输入提示中已经有了一个“q”......如果我想使用 ENTER 而不是 q,那会非常令人不安......有没有办法清除那个“内存”,有一个空的输入字段?

标签: pythoninputkeyboard

解决方案


推荐阅读