[Tutor] loop until a keypress
Peter Otten
__peter__ at web.de
Mon Jan 30 09:10:19 CET 2012
Surya K wrote:
> I want to run code until a "enter" is pressed. Well, it shouldn't wait for
> the user to enter "enter" This is my code:
This is what it looks like over here:
> import msvcrtchr = 0while chr != 'q': print "my code", if
> msvcrt.kbhit(): chr = msvcrt.getch()
This is what I suppose you wrote:
> import msvcrt
> chr = 0
> while chr != 'q':
> print "my code",
> if msvcrt.kbhit():
> chr = msvcrt.getch()
> This isn't working the way
> I wanted. When ever I press enter, the loop is starting in a new line and
> continuing. I even added "break" statement in "if" block but it isn't
> workingCan you tell me how to do that?
You could make a little experiment. Run
import msvcrt
while True:
if msvcrt.kbhit():
print msvcrt.getch()
and then press the 'q' key. What does this little script print? Can you
apply the newly gained information to your original code?
> I am on windows. So, as msvcrt is
> for windows, I wonder if there is any module that works for both,
More information about the Tutor
mailing list