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

Dick Moores rdm at rcblue.com
Wed Oct 31 06:25:45 CET 2007


At 03:49 AM 10/30/2007, Kent Johnson wrote:
>Why do you need to call kbhit() at all? Why not just call getch() 
>and wait for the next character?

Like this?: 
<http://www.rcblue.com/Python/chessTimerForWebV9atest.py>. The 
every-5-seconds time report doesn't report.

Compare

============================
#!/usr/bin/env python
#coding=utf-8
# 21aForTutor.py
import time
import msvcrt
timeNow = time.time()
oldTimeNow = timeNow
while True:
     if msvcrt.kbhit():
         key = msvcrt.getch()
         if key == 'h':
             print 'Hello'
         if key == 'b':
             print 'Bye'
         if key == '\r': # Enter key
             break
     else:
         time.sleep(0.001)
     timeNow = time.time()
     if timeNow - oldTimeNow > 2:
         print "2 seconds passed"
         oldTimeNow = timeNow
==================================
with

===================================
#!/usr/bin/env python
#coding=utf-8
# 21bForTutor.py
import time
import msvcrt
timeNow = time.time()
oldTimeNow = timeNow
while True:
     #if msvcrt.kbhit():
     key = msvcrt.getch()
     if key == 'h':
         print 'Hello'
     if key == 'b':
         print 'Bye'
     if key == '\r': # Enter key
         break
     else:
         time.sleep(0.001)
     timeNow = time.time()
     if timeNow - oldTimeNow > 2:
         print "2 seconds passed"
         oldTimeNow = timeNow
=====================================

In 21bForTutor.py the 2-second report is screwed up. Maybe you can fix it?

Dick




More information about the Tutor mailing list