I\'m running python 2.7 on 64-bit Windows 7.
Here is the code i\'m executing:
import sys
while True:
print \'please enter a character:\'
c = sys.
sys.stdin
is line-buffered by default i.e., your sys.stdin.read(1)
won't return until there is full line in stdin buffer.
It means if you enter a character and hit Enter then after you get the first character with sys.stdin.read(1)
, there is a newline in the buffer (one or two characters: os.linesep
) that are read immediately on the next loop iterations.
You could avoid hitting Enter by reading exactly one character at a time (msvcrt.getch()
).
Problem is probably due to flushing of stdin since the \n
lingers on.
as an alternative, use raw_input
while True:
c = raw_input('please enter a character: ')
print 'you entered', c
For the flushing part, see this