Python3input()似乎在两次调用之间采用旧的 std 输入input()。有没有办法忽略旧输入,只接受新输入(在input()被调用后)?import timea = input('type something') # type "1"print('\ngot: %s' % a)time.sleep(5) # type "2" before timer expiresb = input('type something more')print('\ngot: %s' % b)输出:$ python3 input_test.pytype somethinggot: 1type something moregot: 2
1 回答
哆啦的时光机
TA贡献1779条经验 获得超6个赞
您可以在第二个之前刷新输入缓冲区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)
添加回答
举报
0/150
提交
取消