By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

mysteriously nonfunctioning script - very simple

P: n/a
Can anybody help me make sense of the fact that the following script
doesn't work? It's so simple I can't imagine what I'm missing. Any
help will be much appreciated.

Peace,
STM
## ALARM CLOCK:
from time import sleep,time,localtime

wakeuptime = input('hours: '), input('minutes: ')
onehourlater = (wakeuptime[0]+1, wakeuptime[1])
while not wakeuptime < localtime(time())[3:5] < onehourlater:
sleep(3)
print 'PLAY A SOUND FILE'
Jul 18 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
>>>>> se**********@yahoo.com (Sean McIlroy) (SM) wrote:
SM> Can anybody help me make sense of the fact that the following script
SM> doesn't work? It's so simple I can't imagine what I'm missing. Any
SM> help will be much appreciated.


What do you mean `it doesn't work'?
--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: pi**@vanoostrum.org
Jul 18 '05 #2

P: n/a
Sean McIlroy wrote:
Can anybody help me make sense of the fact that the following script
doesn't work? It's so simple I can't imagine what I'm missing. Any
help will be much appreciated.


Always post actual tracebacks of the problem, if
indeed it is producing a traceback. Do this always
by *cut and paste*, not by retyping the text. Make
sure not to remove anything important, and make sure
you are running the actual code you have posted here.

Also always describe the problem in more detail than
just "doesn't work". For all we know, the code runs
fine but its output just doesn't suit you...

-Peter
Jul 18 '05 #3

P: n/a
On 25 Mar 2005 22:13:09 -0800, se**********@yahoo.com (Sean McIlroy)
declaimed the following in comp.lang.python:
Can anybody help me make sense of the fact that the following script
doesn't work? It's so simple I can't imagine what I'm missing. Any
help will be much appreciated.

Peace,
STM
## ALARM CLOCK:
from time import sleep,time,localtime

wakeuptime = input('hours: '), input('minutes: ')
Use of input() is not recommended... Use raw_input() and
explicitly convert the data.
onehourlater = (wakeuptime[0]+1, wakeuptime[1])
while not wakeuptime < localtime(time())[3:5] < onehourlater:
How about putting some parentheses on that... I tend to read it
as (not wakeuptime) < ...
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #4

P: n/a
Fair enough. Here's the verbose version:

################################################## ####################
from time import sleep,time,localtime

wakeuptime = (7,00)
## I WANT TO BE WOKEN UP AT 7AM (FOR EXAMPLE)

onehourlater = (wakeuptime[0]+1, wakeuptime[1])
## ONE HOUR LATER THAN THAT IS 8AM

while not wakeuptime < localtime(time())[3:5] < onehourlater: sleep(3)
## CHECK THE CURRENT TIME EVERY 3 SECONDS, AND IF IT'S NOT BETWEEN
## 7AM AND 8AM, GO BACK TO SLEEP FOR ANOTHER 3 SECONDS

## CONTROL NEVER REACHES THIS POINT
################################################## ####################




Peter Hansen <pe***@engcorp.com> wrote in message news:<cc********************@powergate.ca>...
Sean McIlroy wrote:
Can anybody help me make sense of the fact that the following script
doesn't work? It's so simple I can't imagine what I'm missing. Any
help will be much appreciated.


Always post actual tracebacks of the problem, if
indeed it is producing a traceback. Do this always
by *cut and paste*, not by retyping the text. Make
sure not to remove anything important, and make sure
you are running the actual code you have posted here.

Also always describe the problem in more detail than
just "doesn't work". For all we know, the code runs
fine but its output just doesn't suit you...

-Peter

Jul 18 '05 #5

P: n/a
> ################################################## ####################
from time import sleep,time,localtime

wakeuptime = (7,00)
## I WANT TO BE WOKEN UP AT 7AM (FOR EXAMPLE)

onehourlater = (wakeuptime[0]+1, wakeuptime[1])
## ONE HOUR LATER THAN THAT IS 8AM

while not wakeuptime < localtime(time())[3:5] < onehourlater: sleep(3)
## CHECK THE CURRENT TIME EVERY 3 SECONDS, AND IF IT'S NOT BETWEEN
## 7AM AND 8AM, GO BACK TO SLEEP FOR ANOTHER 3 SECONDS


It does work for me:
wakeuptime = (19,00)
onehourlater = (20,00)
lt = (18,59)
not wakeuptime < lt < onehourlater True
lt = (19:01)
not wakeuptime < lt < onehourlater

False

And localtime(time())[3:5] returns the desired timetuple. You're sure that's
not working?

--
Regards,

Diez B. Roggisch
Jul 18 '05 #6

P: n/a
Sean McIlroy wrote:
Fair enough. Here's the verbose version: from time import sleep,time,localtime

wakeuptime = (7,00)
## I WANT TO BE WOKEN UP AT 7AM (FOR EXAMPLE)

onehourlater = (wakeuptime[0]+1, wakeuptime[1])
## ONE HOUR LATER THAN THAT IS 8AM

while not wakeuptime < localtime(time())[3:5] < onehourlater: sleep(3)


Maybe you are a little /impatient/ and want to change the above to

while not wakeuptime <= localtime(time())[3:5] < onehourlater: sleep(3)

That should wake you up roughly one minute earlier, and thus before you hit
Ctrl+C ...again.

Peter

Jul 18 '05 #7

P: n/a
Sean McIlroy wrote:
Fair enough. Here's the verbose version:

################################################## ####################
from time import sleep,time,localtime

wakeuptime = (7,00)
## I WANT TO BE WOKEN UP AT 7AM (FOR EXAMPLE)

onehourlater = (wakeuptime[0]+1, wakeuptime[1])
## ONE HOUR LATER THAN THAT IS 8AM

while not wakeuptime < localtime(time())[3:5] < onehourlater: sleep(3)
## CHECK THE CURRENT TIME EVERY 3 SECONDS, AND IF IT'S NOT BETWEEN
## 7AM AND 8AM, GO BACK TO SLEEP FOR ANOTHER 3 SECONDS

## CONTROL NEVER REACHES THIS POINT
################################################## ####################


Works for me in the following slightly-adjusted form (easier for testing):

def alarm(wakeuptime):
#wakeuptime = input('hours: '), input('minutes: ')
onehourlater = (wakeuptime[0]+1, wakeuptime[1])
while not wakeuptime < localtime(time())[3:5] < onehourlater:
sleep(3)
print 'PLAY A SOUND FILE'
print localtime(time())[3:5]
localtime(time()) (2005, 3, 26, 11, 37, 58, 5, 85, 0) alarm((11,38)) PLAY A SOUND FILE
(11, 39)
import sys
sys.version '2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)]' sys.platform 'win32'


Michael
Jul 18 '05 #8

P: n/a
Am Samstag, 26. März 2005 19:47 schrieb Sean McIlroy:
<snip>


Why not try the following:
import datetime
x = datetime.datetime.now()
x datetime.datetime(2005, 3, 26, 21, 48, 13, 495765) y = datetime.datetime(2005,3,26,21,50) # Wake me up today at 21:50
y datetime.datetime(2005, 3, 26, 21, 50, 13) time.sleep((y-x).days*60*60*24+(y-x).seconds+(y-x).microseconds*0.000001)
print "I am past the timeout!"


--
--- Heiko.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBCRcumf0bpgh6uVAMRAldgAJ97TtSMB3UtpMzJBXkNIW B1s6uxGwCdGR6u
zwX3yJ4CP/sKWmVJBM23LnQ=
=Lfc2
-----END PGP SIGNATURE-----

Jul 18 '05 #9

P: n/a
Heiko Wundram <mo*******@ceosg.de> wrote in message news:<ma*************************************@pyth on.org>...

<snip>
Why not try the following:

<snip>

I did try it, and it didn't work either. It appears there must be
something wrong with my computer, hopefully something benign. Thanks
anyway.

Peace,
STM
Jul 18 '05 #10

P: n/a
Sean McIlroy wrote:
I did try it, and it didn't work either. It appears there must be
something wrong with my computer, hopefully something benign.


Just a thought: Is your computer's clock set correctly?

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
Jul 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.