[Tutor] I am puzzled - help needed
Alan Gauld
alan.gauld at freenet.co.uk
Fri Apr 1 10:49:04 CEST 2005
> from time import *
>
> n = time()
> s = str(n)
> numb = s[-2:] # last two characters of the string
> numb = int(numb) # convert back to a number
> guess = (raw_input('Enter a number: '))
You need to convert the string returned by raw_input() into a number
guess = int(raw_input('Enter a number: '))
otherwise you are comparing a string with anumber.
> if guess == numb:
> print ("Bravo, you have just won the right to play again!")
>
> elif guess < numb:
> print "You are just a bit too low, try again"
>
> else:
> print "You are just a bit too high, try again"
>
> print "The End"
> --------------------------------------------------------------------
--------------------------------
> If i write the following line:
> "n = time.time()
>
> I get the following error message:
> AttributeError: 'builtin_function_or_method' object has no attribute
'time'
Thats because you used from time import *
So when you type time.time Python sees the first 'time' as being the
time modules time() function. When you type time.tuime it tries to
access a time attribute of the time function. That doesn't exist
which is what the error says...
Use either
import time
t = time.time()
OR
from time import time
t= time()
Its considered bad practice to use
from time import *
because its easy to get collisions between names in the time module
and other names in your program.
> I feel like an idiot asking what is probably very basics questions
but my
> desire to learn is quite high right now and I don't want to lose it,
thanks
> again
Ask away, thats what the list is for. None of your questions have been
in any way stupid. And we do try to answer stupid questions too! :-)
Alan G.
More information about the Tutor
mailing list