[Tutor] Re:Help on Tutor

Scott Widney SWidney@ci.las-vegas.nv.us
Mon Jul 28 11:31:01 2003


> >>>import time
> >>>mytime = time.localtime()
> >>>mytime
> (2003, 7, 27, 10, 54, 38, 6, 208, 1)
> >>>help(time.localtime)
> Help on built-in function localtime:
> 
> localtime(...)
>     localtime([seconds]) -> 
> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)
> 
>     Convert seconds since the Epoch to a time tuple 
> expressing local time.
>     When 'seconds' is not passed in, convert the current time instead.
> 
> >>>number = mytime[4]
> >>>number
> 54

Two things: first, the fifth element in the list is the current minute, not
the second! You'll approach "randomness" more closely if you use the current
second. So if you're going to use time.localtime(), use index [5].

Second, other's here have pointed out that you'll only get integers in the
range 0-59. Kinda cuts down on the usefulness of using seconds. However,
using time.time() will give you better results and it's where I think the
exercise was heading. Take a look:


>>> import time
>>> help(time.time)
Help on built-in function time:

time(...)
    time() -> floating point number

    Return the current time in seconds since the Epoch.
    Fractions of a second may be present if the system clock provides them.

## This looks promising. Does my clock provide fractions?
>>> help(time.time())
Help on float:

1059404203.9859999

## Yep. So I'll have to deal with that.
## The easiest way would be to just get rid of the fractional part
>>> int(time.time())
1059404248
## And it would be much easier to get the last two digits
##  if I was dealing with a string.
>>> str(int(time.time()))
'1059404275'
## Great! Now I just need the last two characters.
>>> str(int(time.time()))[-2:]
'89'
## There. Now I can compare that to the string that the user enters.
## Oh, and if I need that to be a number again...
>>> int(str(int(time.time()))[-2:])
39
## I'll run it a few more times to be sure it works.
>>> int(str(int(time.time()))[-2:])
42
>>> int(str(int(time.time()))[-2:])
46
>>> int(str(int(time.time()))[-2:])
94
>>> int(str(int(time.time()))[-2:])
1
>>>
## Yep. We get a number in the range 0-99. Much better fit for the exercise