On Dec 5, 2:44 pm, Maxim Khitrov <mkhit...@gmail.com> wrote: > On Fri, Dec 4, 2009 at 6:55 PM, northof40 <shearich...@gmail.com> wrote: > > On Dec 5, 12:52 pm, northof40 <shearich...@gmail.com> wrote: > >> Hi - I'm writing a *very* simple program for my kids. It asks the user > >> to give it the answer to a maths question and says "right" or "wrong" > > >> They now want a timed version where they would only get so long to > >> respond to the question. > > >> I'm thinking of some logic where a raw_input call is executed and then > >> if more than X seconds elapses before the prompt is replied to the > >> process writes a message "Sorry too slow" (or similar). > > >> I can't see the wood for the trees here - what's the best way to do > >> this given the rather simple environment it's needed within. > > >> Regards > > >> richard. > > > Sorry I should said that based upon other answers I've seen to similar > > questions this needs to run on a windows machine (other answers > > suggest this is more difficult than running on *nix) > > Simplest solution I could come up with. This is indeed much easier on > *nix (just use select.select on sys.stdin with a timeout). > > --- > from msvcrt import getch, kbhit, putch > from time import sleep, time > > ans = '' > end = time() + 5 > > print('2 + 2 = ?') > > while True: > while time() < end: > if kbhit(): > break > else: > sleep(0.001) > else: > ans = None > break > > char = getch() > if char == '\r': > print('') > break > ans += char > putch(char) > > if ans is None: > print('\nSorry too slow') > else: > try: > print('right' if int(ans) == 4 else 'wrong') > except: > print('not a number') > --- > > - Max
That's really great - thanks. I've never looked at the whole msvcrt module before - it looks like it could be useful ... at least for windows programmes. Thanks again. R. -- http://mail.python.org/mailman/listinfo/python-list