python - python input() 在调用 input() 之前采用旧的标准输入
问题描述
Python3input()
似乎在两次调用input()
. 有没有办法忽略旧的输入,只接受新的输入(在input()
被调用之后)?
import time
a = input('type something') # type "1"
print('\ngot: %s' % a)
time.sleep(5) # type "2" before timer expires
b = input('type something more')
print('\ngot: %s' % b)
输出:
$ 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)
推荐阅读
- c# - VS2017 Winfroms 无法更新服务参考
- google-chrome - material-ui:Chrome 意外自动填充
我试图阻止 Chrome 自动填充我的所有字段。我将 autocomplete='off' 添加到 TextField 和 Autocomplete 的输入中,如下所示:
<TextField autoComplete={'off'} <Autocomplete renderInput={par
- python - 如何做python顶级应用程序,但是当你打开一个新程序时,它会下降1级
- javascript - 如果存在于 mongodb 的数组中,则删除数组
- swift - 从循环中创建具有多个子元素的可编码结构
- python - 如何使用多线程来简化以下 python 代码
- c++ - 从 C++ 中的函数返回 this
- java - JAVA中的equals()方法
- laravel - Laravel Cashier 重用支付意图
- java - 需要帮助了解 Tomcat 与 WepApp 类加载问题