I wrote a function that accepts a character (without hitting enter
), and checks for validation, and returns the key pressed. But the problem is, the prompt I am printing is printing it two times if the value is not matched. Here is my code.
def accept_input():
while True:
print "Type Y to continue, ctrl-c to exit"
ch = sys.stdin.read(1)
if ch != "Y":
pass
else:
return ch
and when called accept_input()
, it is printing the prompt twice when there is a non matching character, and printing once if the input is blank.
python accept_input.py
Type Y to continue, ctrl-c to exit
a
Type Y to continue, ctrl-c to exit
Type Y to continue, ctrl-c to exit
b
Type Y to continue, ctrl-c to exit
Type Y to continue, ctrl-c to exit
c
Type Y to continue, ctrl-c to exit
Type Y to continue, ctrl-c to exit
Type Y to continue, ctrl-c to exit
Type Y to continue, ctrl-c to exit
Y
accepted
Why is it printing twice when entering any non matching key, and why is it printing only once when a blank key is entered?
Thanks.