[Tutor] Using the time module to extract a semi-random number

Patrick Sabin patrick.just4fun at gmail.com
Thu Sep 17 10:50:11 CEST 2009


Laurii wrote:
> Hello all,
> 
> I am currently reading through the Tutorial for Non-Programers by Josh 
> Cogliati.  I have had great success until now.
> 
> The exercise to modify a number guessing program from a fixed number 
> "number = 78" to using the time module and use the seconds at the time 
> the program is used to be the number. (i.e. if the clock on your 
> computer says 7:35:25 then it would take the 25 and place it in "number".
> 

You can either use:

import time
number = int(time.strftime("%S"))

or use real pseudo-random numbers:

import random
number = random.randint(0,59)

The latter looks clearer to me.

- Patrick


More information about the Tutor mailing list