Skip to content Skip to sidebar Skip to footer

Python Input() Takes Old Stdin Before Input() Is Called

Python3's input() seems to take old std input between two calls to input(). Is there a way to ignore the old input, and take new input only (after input() gets called)? import time

Solution 1:

You can flush the input buffer prior to the second input(), like so

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)

Post a Comment for "Python Input() Takes Old Stdin Before Input() Is Called"