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

Be sure you understand what format the time number has and that you understand the problem statement. Here are two time values:


1112306463.0
1112306463.01

Do you see what is going to happen if you blindly take the last two characters of the time in the first case and try to convert it to an integer? Are you suppose to take the last two digits of the integer portion?

Also, a neat way to get the last 2 digits of a number is to use the "remainder/modulus" operator:

###
>>> from time import *
>>> t=int(time())
>>> t
1112306749
>>> t%10 #last 1
9
>>> t%100 #last 2
49
>>> t%1000 #last 3
749
###


Also, note that when you "import *" you no longer need (or can) use the "time." prefix for times functions; when you import them that way it is as if they are part of your own function set:


###
>>> from time import *
>>> time.time()
Traceback (most recent call last):
File "<input>", line 1, in ?
AttributeError: 'builtin_function_or_method' object has no attribute 'time'
###


..you get this error because time's function "time()" is part of your programs functions; there is not module "time" for you to interact with, only time's functions. So go ahead and just use the function:

###
>>> time()
1112306900.8461709
###

If you import the whole module, though, and try the same thing...

###
>>> import time
>>> time()
Traceback (most recent call last):
  File "<input>", line 1, in ?
TypeError: 'module' object is not callable
###

python knows about the *module* time but you are now treating the module like a function. Instead, you should access the function through the module's name:

###
>>> time.time()
1112306919.518116
###

/c


_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

Reply via email to