[Tutor] Python 3.0

bob gailer bgailer at gmail.com
Sat Feb 20 14:56:30 CET 2010


On 2/20/2010 5:52 AM, Paul Whittaker wrote:
>
> Using the following script, the (Windows 7) PC echoes the key presses 
> but doesn't quit (ever!)
>
> import msvcrt
>
> print('Press SPACE to stop...')
>
> while True:
>
>     k = msvcrt.getch()     # Strangely, this echoes to the IDLE window
>
>     if k == ' ':           # Not recognized
>
>         break
>


According to the docs: "msvcrt.getch() - Read a keypress and return the 
resulting character....This call will block if a keypress is not already 
available, but will not wait for Enter to be pressed. If the pressed key 
was a special function key, this will return '\000' or '\xe0'; the next 
call will return the keycode. The Control-C keypress cannot be read with 
this function."

IOW - getch always returns 1 character.

You are testing for a 0 length string. Will never happen.

What keystroke do you want to break the loop?

-- 
Bob Gailer
919-636-4239
Chapel Hill NC

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100220/c6663213/attachment-0001.htm>


More information about the Tutor mailing list