[Tutor] Newbie question on passing a variable

Daniel Yoo dyoo@hkn.EECS.Berkeley.EDU
Mon, 21 Aug 2000 18:52:51 -0700 (PDT)


On Sun, 20 Aug 2000 FxItAL@aol.com wrote:

> I've written the following to try and get input from the user. This
> works fine, my problem is passing it the variable InputMin in the
> other module - timer1.py.

Dear Al,

I know this will sound silly, but I'm still not quite sure what the
problem is.  I don't mean to be flippant --- I'm just uncertain at what's
happening.  Because of this, I'll concentrate on TimeFunc(), and see if I
can see a bug.


> def TimeFunc(var):
> from time import *  

Just to make sure, the indentation of the 'from' needs to match up with
the rest of the body.  Otherwise, it'll complain about a SyntaxError, I
think.


>     me = time()
>     you=localtime(me)
>     ClockMin=you[4] #Represents Minute on Clock
>     ClockHour=you[3] #Represents Hour on Clock
>     InputMin=var
>     InputHour=you[3]
>     if ClockMin<InputMin:
>         while ClockMin<InputMin or ClockHour<InputHour:             me = time()
>             you=localtime(me)                                       ClockMin=you[4]
>             ClockHour=you[3]
>         print "great"


There's some indentation weirdness going on from my email client, which
could either be in the program itself or in the email message.  Can you
send this section of code again?  Also, try to keep your line lengths
short in the code.

Oh, as a local optimization, you can just do:

  you = localtime(time())

and that will remove the need for a "me" variable.  You might want to
rename 'you' though to something more... um... time-like.  *grin*


From what it looks like, your program is "spin-waiting", which is a term
for looping and looping until something happens.  Be aware that this is
generally not a good thing, because it really slows your computer down.  
You can add in a short interval of time.sleep() as follows:

  sleep(60)

into the while loop, and have it poll every 60 seconds instead of every
few milliseconds.  This is better, since that's how often the minutes and
hours change anyway.


> If I use only these two lines:
> 
> def TimeFunc(var):
>     print var
> it works.


Can you show us the exact error message that happens when you try out that
TimeFunc()?  My brain isn't parsing Python rigidly enough to discover the 
bug, so an error report will make things easier to fix.