Using OS time

Skip Montanaro skip at pobox.com
Fri Jun 20 12:07:41 EDT 2003


    Andy> The problem:

    Andy> I am writing a rather simple guessing game (a random number is
    Andy> generated, user has to guess the number).  I am wanting to
    Andy> introduce a time-limit on the game-play so that the user has -
    Andy> e.g. 60 seconds - within which to guess the correct number
    Andy> otherwise they lose.

How about signal.alarm()?

    alarm(...)
        alarm(seconds)

        Arrange for SIGALRM to arrive after the given number of seconds.

You will obviously have to have a signal handler set up to recover from the
signal.  You'll have to experiment to see if the signal will terminate the
call to raw_input() (or whatever you're using to gather user input).

Skip





More information about the Python-list mailing list