如何在循环中忽略输入[Python]

如何在循环中忽略输入[Python],python,raspberry-pi,Python,Raspberry Pi,我试图弄明白如何在Python中忽略循环的输入 下面的代码是一个简单的Python代码,它接受数字输入作为循环数,并在循环中打印字符 导入时间 而(1):#主回路 x=0 inputValue=输入(“输入一个数字:”) 而(x 我尝试使用刷新或键盘中断,但仍然存在相同的问题。这可能是一个解决方案: import time import sys def flush_input(): try: import msvcrt while msvcrt.kbhi

我试图弄明白如何在Python中忽略循环的输入

下面的代码是一个简单的Python代码,它接受数字输入作为循环数,并在循环中打印字符

导入时间
而(1):#主回路
x=0
inputValue=输入(“输入一个数字:”)
而(x
但是,当您从键盘输入某些内容并在循环进行(循环)时按enter键时,此值将成为下一个循环主循环的输入

我的问题是如何避免或忽略循环中间的输入。< /P>
我尝试使用刷新或键盘中断,但仍然存在相同的问题。

这可能是一个解决方案:

import time
import sys

def flush_input():
    try:
        import msvcrt
        while msvcrt.kbhit():
            msvcrt.getch()
    except ImportError:
        import sys, termios    #for linux/unix
        termios.tcflush(sys.stdin, termios.TCIOFLUSH)

while (1): # main loop
    x = 0
    flush_input()
    inputValue = input("Input a number: ")
    while(x <  int(inputValue)):
        print(x)
        x = x + 1
        time.sleep(1)
导入时间
导入系统
def flush_输入()
尝试:
导入msvcrt
而msvcrt.kbhit():
msvcrt.getch()
除恐怖外:
为linux/unix导入sys,termios
termios.tcflush(sys.stdin,termios.TCIOFLUSH)
而(1):#主回路
x=0
刷新_输入()
inputValue=输入(“输入一个数字:”)
而(x
属性:

可能重复的