Python input () принимает старый стандарт перед вызовом input () - PullRequest
1 голос
/ 05 апреля 2019

Python3 input(), кажется, принимает старый ввод std между двумя вызовами 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

1 Ответ

1 голос
/ 05 апреля 2019

Вы можете очистить входной буфер до второго 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)
...