在 Windows 上的 input() 之前清除输入缓冲区?

Clear input buffer before input() on Windows?

我正在创建一个游戏,您必须在特定时间内猜测某个操作的结果;为此,我创建了一个线程来进行定时输入,然后在时间结束后关闭线程。

我的问题是当用户没有输入任何数字时,在时间到之前,在下一个循环中输入等待两个输入而不是一个。

所以,我需要的是在获取新输入之前清除标准输入。我找到了一些适用于 Unix 的解决方案,但没有找到适用于 Windows 的解决方案,有人可以帮助我吗?

这是我使用的代码:

import os
import random 
from time import sleep
import threading
import concurrent.futures

random.seed()

def get_input(a):
   a[0]=input()

def do_operation(time, n):
    number_1 = random.randint(0,n)
    number_2 = random.randint(0,n)
    operations = ['+', '-', '/', '*']
    operation = random.choices(operations)
    operation = "".join(operation)
    if operation == '/':
        while(number_1 % number_2 != 0):
                number_1 = random.randint(0,n)
                number_2 = random.randint(0,n)
    #Trovo i risultati delle operazioni
    if operation == '+':
        result = number_1 + number_2
    elif operation == '-':
        result = number_1 - number_2
    elif operation == '*':
        result = number_1 * number_2
    elif operation == '/':
        result = number_1 / number_2

    print(number_1,operation,number_2, '=', end = " ")
    count = [None, result]
    x = threading.Thread(target=get_input, args = (count,), daemon=True)
    x.start()
    x.join(time)
    return count


def stampastats(lives, points):
    print('\n\t\t\t\tlives:', end = " ")

    for live in range(lives):
        print('*', end = " ")    

    print('\n\t\t\t\tPoints:', points)

#main
lives = 3
points = 0
while(lives > 0):
    stampastats(lives, points)
    result = do_operation(10, 200)
    if(result[0] != result[1]):
        print('\nWrong')
        print('\nThe result was: ' + str(result[1]))
        lives -= 1
    else:
        print('\nBravo')
        points += 1000
    if(lives == 0):
        print('\nGAME OVER')
    else:
        sleep(1)
        os.system('cls')
    

如果发生超时,则有待处理 input()。在这种情况下,您可以发送 Enter;我正在申请 pyautogui module to do that. (Call is_alive() after join() to decide whether a timeout happened.).

在以下脚本中修复了一些运行时错误(并保留了一些调试输出):

import os
import random 
from time import sleep
import threading
import concurrent.futures
import pyautogui

random.seed()

def get_input(a):
    a[0]=input()

def do_operation(time, n):
    number_1 = random.randint(0,n)
    number_2 = random.randint(0,n)
    operations = ['+', '-', '/', '*']
    operation = random.choices(operations)
    operation = "".join(operation)
    #Trovo i risultati delle operazioni
    if operation == '+':
        resulta = number_1 + number_2
    elif operation == '-':
        resulta = number_1 - number_2
    elif operation == '*':
        resulta = number_1 * number_2
    else: # operation == '/':
        while(number_2 == 0 or number_1 % number_2 != 0):
                number_1 = random.randint(0,n)
                number_2 = random.randint(0,n)
        resulta = number_1 / number_2
    
    print(number_1,operation,number_2, '=', end = " ")
    count = [None, resulta]
    x = threading.Thread(target=get_input, args = (count,), daemon=True)
    x.start()
    x.join(time)
    #print( x.native_id, x.is_alive(), x.isDaemon()) # debugging info 
    if x.is_alive():
        pyautogui.press("enter")               # answer to pending input()
    return count


def stampastats(lives, points):
    print('\n\t\t\t\tlives:', end = " ")
    for live in range(lives):
        print('*', end = " ")    
    print('\n\t\t\t\tPoints:', points)

#main
lives = 3
points = 0
while(lives > 0):
    stampastats(lives, points)
    result = do_operation(10, 200)
    #print(result)
    if((result[0] == None) or (result[0] == '') or (float(result[0]) != float(result[1]))):
        print('\nWrong', result)
        print('The result was: ' + str(result[1]))
        lives -= 1
    else:
        print('\nBravo', result)
        points += 1000
    if(lives == 0):
        print('\nGAME OVER')
    else:
        sleep(1)
        # os.system('cls')