[Tutor] Looking for suggestions for improving chessTimer.py code

Dick Moores rdm at rcblue.com
Sat Oct 27 03:39:57 CEST 2007


At 05:00 AM 10/26/2007, bhaaluu wrote:
>Greetings,
>
>On 10/26/07, Dick Moores <rdm at rcblue.com> wrote:
> > At 09:33 AM 10/25/2007, Dick Moores wrote:
> > >Please give me constructive criticism of
> > ><http://www.rcblue.com/Python/chessTimerForWebV6.py>
> >
> > Hmm. Nothing. One problem may be that only people using Windows can
> > run the script.
>
>The first thing I saw when I looked at the code was that it was
>for MS-Windows only, so I didn't waste anymore time with it.
>
>Recently, I ran across some code that tried MS-Windows, except
>if the user wasn't running MS-Windows, it assumed they were
>running some *nix variant, and loaded those modules instead.
>That certainly makes code more platform independent.
>
>This is what it looked like:
>try:
>     # windows or dos
>     import msvcrt
>     getkey = msvcrt.getch
>except ImportError:
>     # assume unix
>     import tty, termios

OK, here's a short script, which runs fine on Windows. What do I add 
to have it run on unix as well?

#!/usr/bin/env python
#coding=utf-8

try:
     # windows or dos
     import msvcrt
     while True:
         if msvcrt.kbhit():
             key = msvcrt.getch()
             if key == "h":
                 print "Hello, World!"
                 break
     print "Bye, World!"

except ImportError:
     # assume unix
     import tty, termios

Dick



More information about the Tutor mailing list