John Carmona wrote:
I have going through Josh Cogliati tutorial, I am stuck on one of the exercise. I need to rewrite the high_low.py program (see below) to use the last two digits of time at that moment to be the "random number". This is using the import time module.

If you look at the docs for the time module, you will see that time.time will return the number of seconds since the epoch.


>>> 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.

>>> time.time()
1112215685.8956151

The last two digits here will be changing very frequently, so they make a reasonable choice as a simple sort of random number.

But how do you get the last two?

If we had the number as a string (ie: "1112215685.8956151"), we could use string slicing to extract the end: if s = "1112215685.8956151" then s[-2:] will be "51".

So, to get your random number, you will need to:
 - Convert the current time into a string.
 - Get the last two characters.
 - Convert them into an integer.

HTH!

--
John.
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to