comparing three variables on if statement - python

I need some help with get pass on the if statement. I have a problem with if statement as I am trying to get pass when I am trying to compare on the values using three variables.
start_time = '22:35'
current_time = '23:48'
stop_time = '00:00'
if current_time == start_time:
print "the program has started"
elif start_time != current_time < stop_time:
print "program is half way"
elif current_time > stop_time:
print "program has finished"
I can get pass on each if statement with no problem, but my problem is when I have the variable start_time with the value 22:35 which it is not equal to current_time value 23:48. So how I can compare with the values between the start_time and current_time as I want to compare to see if it is less than the value 00:00 from the variable stop_time?
I want to check if the value from the variable stop_time which is less than the current_time and start_time.
Just imagine you are watching the tv program which it start at 22:35 in your current time. You are watching which it is less than before the program finish at 00:00, so you check the time again later before the finish time 00:00, it say it is still half way through. Then you check it later at your current time which it is after than 00:00 so it say the program has finished.
Well on my code, I will always get pass on the elif current_time > stop_time: as i always keep getting the print "program has finished" which I should have print "program is half way" instead.
How can you compare three values between the variables start_time and current_time to see if it is less than and check to see if it is less the value 00:00 from the variable stop_time?
EDIT: Here is what I use them as a string when i am getting the hours and minutes from the date format especially year, month, day, hours, minutes and seconds
start_date = str(stop_date[0]) #get start_date date format from database
stop_date = str(stop_date[1]) #get start_date date format from database
get_current_time = datetime.datetime.now().strftime('%H:%M')
get_start_time = time.strptime(start_date, '%Y%m%d%H%M%S')
start_time = time.strftime('%H:%M', get_start_time)
get_stop_time = time.strptime(stop_date, '%Y%m%d%H%M%S')
stop_time = time.strftime('%H:%M', get_stop_time)
current_time = str(get_current_time)

Using two comparisons and the and logical we could create something like this:
if current_time > start_time and current_time < stop_time:
#do work
But that actual problem is dealing with date and time
current_time = datetime.now() #lets say this is 2016, 2, 12, 17:30
start_time = datetime.datetime(2016,2,12,16,30) # or datetime.strptime(start_time)
end_time = datetime.datetime(2016,2,13,0,0)
if current_time == start_time:
print "the program has started"
elif current_time > start_time and current_time < stop_time:
print "Program is still running"
elif current_time > stop_time:
print "program has finished"

Related

Why isn't my alarm code printing the message at the end?

So I'm fairly new to Python and I am trying to write a code for a timer. My code is supposed to get the hour, minute, whether it's AM or PM, and a message that they want the program to print out when their timer is done. As of now, the program asks several questions and stores them in variables, but doesn't print out the message when it is done.
I have tried looking at each part of the code and the code is fairly simple so I don't understand why this is occurring.
# Set up variables
hour = int(input('What should the hour be? '))
minute = input('What should the minute be? ')
ampm = input('A.M. or P.M.? ')
if (ampm == 'A.M.'):
if (hour == 12):
hour = 0
else:
hour = hour
if (ampm == 'P.M.'):
if (hour == 12):
hour = hour
else:
hour = hour + 12
message = input('What should the message be? ')
import datetime
current_hour = datetime.datetime.today().strftime('%H')
current_minute = datetime.datetime.today().strftime('%M')
alarm = True
# Set up loop
while (alarm):
if (hour != datetime.datetime.today().strftime('%H')):
alarm = True
else:
if (minute == datetime.datetime.today().strftime('%M')):
print(message)
alarm = False
else:
alarm = True
It is supposed to print out the message that the user inputted. It's not doing that.
The variable of hour returns an int value and datetime.datetime.today().strftime('%H') returns string so your program get in the infinite loop.Change condition like
if (hour != int((datetime.datetime.today().strftime('%H')))):

While Loop is not working well with the time

I have a code with this ↓ part in it, which should do something when the time and day come. It has outer and inner time and day checks (while loops).
Outer for the situation in which code starts processing in time (or day) which is not in the timerange for code processing - it should wait until that time (or day). And it works, but only in situation when I set it like 10 minutes before the timerange (code processing time). When I set it like a night before, it won't works and it stops in print("timerange wait") It shows False even it should be True. Is there a problem because it is not in the same day? Or something else? Please give me some advices.
Here is the simple version of code (without unnecessary parts )
import os
import datetime
from time import sleep
from datetime import date
#setting used variables
now = datetime.datetime.now()
morning = now.replace(hour=8, minute=0, second=0, microsecond=0)
evening = now.replace(hour=18, minute=9, second=0, microsecond=0)
# function for check creating "timerange -start time, end time"
def time_in_range(morning, evening, x):
if morning <= evening:
return morning <= x <= evening
else:
return morning <= x or x <= evening
# main program for doing something
def main():
while True:
# Time period check - Check if time and weekdays are not out of range
# if not in range will wait till the time
if date.today().weekday():
dayz = True
else:
dayz = False
now = datetime.datetime.now()
timerange = time_in_range(morning, evening, now)
if timerange != True:
print("waiting for function-time")
sleep(10)
continue
if dayz != True:
print("waiting for function-days")
sleep(3600)
continue
while True:
print("do something")
#check if during while loop there wasn't date, time change (out of timerange etc)
if date.today().weekday():
dayz = True
else:
dayz = False
now = datetime.datetime.now()
timerange = time_in_range(morning, evening, now)
if timerange != True:
print("break inner loop-timerange")
break
if dayz != True:
print("break inner loop-dayz")
break
print("do something -main work")
#program starts here
timerange = time_in_range(morning, evening, now)
# check if today is weekday (outer loop)
if date.today().weekday():
dayz = True
else:
dayz = False
# if today is not a weekday wait till weekday (outer loop)
#for situation where the code started not in weekday
while dayz != True:
print("days wait")
sleep(3600)
dayz = date.today().weekday()
if dayz == True:
break
# If it is not processing time, wait until it becomes
# for the situation where code started not in "timerange"
while timerange != True:
print("timerange wait") # ---------HERE IT STOPS---------
# But it works fine, when I set it like one hour before 'timerange' or so...
sleep(10)
now = datetime.datetime.now()
timerange = time_in_range(morning, evening, now)
print(timerange)
if timerange == True:
break
# Do the main function
main()
# Let know when code ends
print("Over of Code")

Python -While loop doesn't work with Time

I have a code which starts a main function. In this function have a while loop which should starts program when certain time comes. I have set this time in morning (start time), evening(end time) variables. It is in while loop and it works, but only if I start the program the day I want to use it. For example: When I start it Monday evening (20:00) and start time(morning variable) is from 8:00 (next day), it will continue loop
print("Waiting for the right time") <=(doing this)
even if that time the next day comes. But It works when I start it the next day at 6:00 or so...
Can someone explain me, why this happens?
Here is the code
import datetime
from time import sleep
from datetime import date
#variables
now = datetime.datetime.now()
morning = now.replace(hour=8, minute=0, second=0, microsecond=0)
evening = now.replace(hour=16, minute=15, second=0, microsecond=0)
#function for time-setting
def time_in_range(morning, evening, x):
if morning <= evening:
return morning <= x <= evening
else:
return morning <= x or x <= evening
timerange = time_in_range(morning, evening, now)
#main function
def main():
while True:
# Time period check
if date.today().weekday() < 5 and date.today().weekday() >= 0:
dayz = True
else:
dayz = False
if dayz != True:
print("Waiting for the day")
sleep(3600)
continue
now = datetime.datetime.now()
timerange = time_in_range(morning, evening, now)
if timerange != True: # HERE IT MAKES THE TROUBLE
print("Waiting for the right time")
sleep(200)
continue
print("do something")
main()
print("end of code")
When you call .replace() to set the morning and evening times, it keeps the current date as part of the datetime object. So if you were to call it a day before, the dates would be set to the previous day's date, and thus .now() will never be in between the previous day's time range.
E.g. if on January 1st you make the calls to set morning and evening, the stored datetimes will be "January 1st 8am" and "January 1st 4pm". The next time when your loop is checking, it asks "Is January 2nd 10am between January 1st 8am and January 1st 4pm" and of course the answer is no, because January 1st was the day before.
You probably want to use the datetime.time class instead of the datetime.datetime class, if you're only wanting to check for time. Alternatively, you could set the date portion of your evening and morning datetimes to the specific date you want to match (but that wouldn't help for repeating weekly).
import datetime
from time import sleep
from datetime import date
#variables
now = datetime.datetime.now()
morning = now.replace(hour=8, minute=0, second=0, microsecond=0)
evening = now.replace(hour=16, minute=15, second=0, microsecond=0)
#function for time-setting
def time_in_range(morning, evening, x):
# Updated code
morning = x.replace(hour=8, minute=0, second=0, microsecond=0)
evening = x.replace(hour=16, minute=15, second=0, microsecond=0)
if morning <= evening:
return morning <= x <= evening
else:
return morning <= x or x <= evening
timerange = time_in_range(morning, evening, now)
print(timerange)
#main function
def main():
while True:
# Time period check
if date.today().weekday() < 5 and date.today().weekday() >= 0:
dayz = True
else:
dayz = False
if dayz != True:
print("Waiting for the day")
sleep(3600)
continue
now = datetime.datetime.now()
timerange = time_in_range(morning, evening, now)
if timerange != True: # HERE IT MAKES THE TROUBLE
print("Waiting for the right time")
sleep(200)
continue
print("do something")
main()
print("end of code")

now_time is not getting updated while code is looping

i'm trying to do a loop, that keeps checking if time is between 2 times.. so it turns my lights on. but while its looping the current time does not get updated. im trying to create a aquarium controller, that turns 3 sets of lights on a different times.
thanks for your help :)
from datetime import datetime, time
now = datetime.now()
now_time = now.time()
SleepTimeL = 2
if now_time >= time (9,30) and now_time <= time (16,15):
print "yes, within the interval"
print now_time
time.sleep( 9 )
else:
print "no"
You'll need to keep updating now_time in your loop:
while True:
if time (9,30) <= now_time <= time (16,15):
print "yes, within the interval"
now_time = datetime.now().time()
print now_time
time.sleep( 9 )
else:
print "no"
You can replace the conditions with a chained comparison, which is more readable.
You can also set the comparison as the condition on the while, in which case the loop only runs in the specified duration and needs to be restarted for the cycle of the duration:
while time (9,30) <= now_time <= time (16,15):
print "yes, within the interval"
now_time = datetime.now().time()
print now_time
time.sleep( 9 )
If you want to do a loop, you should use one, like while.
from datetime import datetime, time
now = datetime.now()
now_time = now.time()
SleepTimeL = 2
print time(19,30)
print now_time
while True:
if now_time >= time (9,30) and now_time <= time (16,15):
print "yes, within the interval"
print now_time
time.sleep( 9 )
else: print "no"
If you want to make an aquarium controller you should check the lights state in every iteration:
from datetime import datetime, time
now = datetime.now()
now_time = now.time()
SleepTimeL = 2
print time(19,30)
print now_time
while True:
if now_time >= time (9,30) and now_time <= time (16,15):
if isLightOff: lightOn
time.sleep( 9 )
else: if isLightOn: lightOff

Python 2.7 .datetime.datetimenow() and datetime.timedelta()

I have this following code and am stuck in the while loop
I know there is a problem with the while datetime.datetime.now() < (datetime.datetime.now() + datetime.timedelta(minutes=wait_time)): line.
Can anyone help please ?
nodes_with_scanner = []
wait_time = 60
while datetime.datetime.now() < (datetime.datetime.now() + datetime.timedelta(minutes=wait_time)):
nodes_with_scanner = get_nodes_with_scanner_in_dps(self.node_names, scanner_id, username=self.users[0].username)
log.logger.debug("Number of pre-defined {0} scanners detected in DPS: {1}/{2}".format(scanner_type, len(nodes_with_scanner), len(self.node_names)))
if state == "create":
if len(self.node_names) == len(nodes_with_scanner):
log.logger.debug("All {0} pre-defined scanners with id '{1}' have been successfully created in DPS for nodes '{2}'".format(scanner_type, scanner_id, ", ".join(self.node_names)))
return
elif state == "delete":
if len(nodes_with_scanner) < 1:
log.logger.debug("All {0} pre-defined scanners with id '{1}' have been successfully deleted in DPS for nodes '{2}'".format(scanner_type, scanner_id, ", ".join(self.node_names)))
return
log.logger.debug("Still waiting on some {0} pre-defined scanners to '{1}' in DPS; sleeping for 1 minute before next check".format(scanner_type, state))
time.sleep(60)
You are asking if the current time is smaller than the current time plus a delta. Of course that's going to be true each and every time, the future is always further away into the future.
Record a starting time once:
start = datetime.datetime.now()
while datetime.datetime.now() < start + datetime.timedelta(minutes=wait_time)):
If wait_time doesn't vary in the loop, store the end time (current time plus delta):
end = datetime.datetime.now() + datetime.timedelta(minutes=wait_time))
while datetime.datetime.now() < end:
It may be easier to just use time.time() here:
end = time.time() + 60 * wait_time
while time.time() < end:
You use datetime.datetime.now() in your while loop, what means each iteration you check if the time now is lower then the time now + delta.
That logically wrong, because it will be True forever as the time now will be always lower than the time now plus delta.
You should change it to this:
time_to_start = datetime.datetime.now()
while datetime.datetime.now() < (time_to_start + datetime.timedelta(minutes=wait_time)):
print "do something"

Categories