python input()在调用input()之前接受旧的stdin

python input()在调用input()之前接受旧的stdin,python,python-3.x,input,io,stdin,Python,Python 3.x,Input,Io,Stdin,Python3的input()似乎在两次调用input()之间使用旧的std输入。有没有一种方法可以忽略旧的输入,而只接受新的输入(在调用input()之后) 输出: $ python3 input_test.py type something got: 1 type something more got: 2 您可以在第二次input()之前刷新输入缓冲区,如下所示 import time import sys from termios import tcflush, TCIFLUSH

Python3的
input()
似乎在两次调用
input()
之间使用旧的std输入。有没有一种方法可以忽略旧的输入,而只接受新的输入(在调用
input()
之后)

输出:

$ python3 input_test.py
type something
got: 1

type something more
got: 2

您可以在第二次
input()
之前刷新输入缓冲区,如下所示

import time
import sys
from termios import tcflush, TCIFLUSH

a = input('type something') # type "1"
print('\ngot: %s' % a)

time.sleep(5) # type "2" before timer expires

tcflush(sys.stdin, TCIFLUSH) # flush input stream

b = input('type something more')
print('\ngot: %s' % b)
import time
import sys
from termios import tcflush, TCIFLUSH

a = input('type something') # type "1"
print('\ngot: %s' % a)

time.sleep(5) # type "2" before timer expires

tcflush(sys.stdin, TCIFLUSH) # flush input stream

b = input('type something more')
print('\ngot: %s' % b)