Comparing three variables in an if statement

I need help getting a pass in an if statement. I have a problem with the if statement, as I try to get a pass when I try to compare 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 a pass for each if statement without problems, but my problem is that I have a start_time variable with a value of 22:35 , which is not equal to the current_time value of 23:48 . So, how can I compare with the values ​​between start_time and current_time , how do I want to compare to see if this value is 00:00 from the stop_time variable?

I want to check if the value of the stop_time variable stop_time less than current_time and start_time .

Imagine that you are watching a program on TV that starts at 22:35 at the current time. You see that it’s less than before the end of the program at 00:00 , so you check the time later before the end of the time 00:00 , he says that it is still halfway. Then you check it later at the current time, which is after 00:00 , so it says that the program is completed.

In my code, I always get a pass elif current_time > stop_time: since I always get print "program has finished" , which should have print "program is half way" .

How can you compare the three values ​​between start_time and current_time to see if it is less and check if it is less than 00:00 from the stop_time variable?

EDIT: this is what I use as a string when I get hours and minutes from a 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) 
+5
source share
1 answer

Using two comparisons and a logical and , we could create something like this:

 if current_time > start_time and current_time < stop_time: #do work 

But this actual problem is related to the 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" 
0
source

Source: https://habr.com/ru/post/1242926/


All Articles