[Tutor] Looking for suggestions for improving chessTimer.py code
Dick Moores
rdm at rcblue.com
Mon Oct 29 13:46:42 CET 2007
At 05:03 AM 10/29/2007, bhaaluu wrote:
>On 10/29/07, Dick Moores <rdm at rcblue.com> wrote:
> > Seems I should clarify that. I'm not looking for a script that echoes
> > the key pressed. So I'll change that last line to "Could you show a
> > script using it that, say, prints "Hello" if '1' is pressed and
> > "Bye" if '2' is, AND uses
> > <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892>
> > Dick
>
># tty-example-2.py
># Tue Oct 19 09:07:14 CEST 1999
># Fredrik Lundh fredrik at pythonware.com
># http://mail.python.org/pipermail/python-list/1999-October/014151.html
>import sys
>
>try:
> # windows or dos
> import msvcrt
> getkey = msvcrt.getch
>except ImportError:
> # assume unix
> import tty, termios
>
> print dir(termios)
>
> def getkey():
> file = sys.stdin.fileno()
> mode = termios.tcgetattr(file)
> try:
> tty.setraw(file, termios.TCSANOW)
> ch = sys.stdin.read(1)
> finally:
> termios.tcsetattr(file, termios.TCSANOW, mode)
> return ch
>
>print "press 'q/p/r/s' to quit..."
>
>while 1:
> ch = getkey()
> if ch == "q":
> break
> elif ch == "1":
> print "Hello",
> elif ch == "2":
> print "Bye",
> print ch,
>
>print
>
>
>Like that?
Thanks! I modified it slightly:
<http://www.rcblue.com/Python/toTestOnUnix.py>. Could a unix/linux
user see if it works?
Dick
More information about the Tutor
mailing list