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

Mal Wanstall m.wanstall at gmail.com
Thu Sep 17 03:41:59 CEST 2009


Hi Katt,

The following does the trick for me:

number = int(time.strftime("%S", time.localtime()))

There may be better ways to do it though.

-Mal

On Thu, Sep 17, 2009 at 8:43 AM, Laurii <the_only_katala at verizon.net> 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".
>
> The following is what I have so far:
> #*********************************************
> #
> #    hilow2.py
> #
> #    This program asks the user to guess a
> #    number.  If it is wrong the program tells
> #    the user if it is higher or lower.  The
> #    first one had a fixed assigned number.
> #    This program takes the last two digits
> #    of the time and assigns it as the random
> #    number.
> #
> #*********************************************
> #*****************.h header files******************
> #*********************************************
> #******************modules*********************
> from time import time, ctime
> #*********************************************
> #**************define lists/dictionaries**************
> #*********************************************
> #****************define functions*****************
> #*********************************************
> #*************define global variables***************
> the_time = ctime()
> number = the_time
> guess = 0
> guess_try = 0
> #*********************************************
> #****************Main Program******************
> while guess != number:
>    guess = input("Guess a number:")
>    if guess != number:
>         if guess > number :
>              print "To High"
>         elif guess < number:
>              print "To Low"
>    guess_try = guess_try + 1
> print "Bingo! You are correct."
> print "It only took you",guess_try,"tries."
> #*********************************************
>
> I think it has to do with the fact that I am using the wrong time function
> and am not familiar with how to cut out the seconds and send it to the
> variable number.
>
> I believe I should use the strftime(format[,t]) but am not sure how to.  All
> help is appreciated.
>
> Thanks in advance,
>
> Katt
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list