简体   繁体   中英

python error invalid syntax on if statement

I need some help with my code. I want to check if the variable start_time is less than, equal or greater than the current_time to compare for the time.

Here is the code:

start_date = str(stop_date[0])
stop_date = str(stop_date[1])
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)

if start_time <> current_time <> stop_time:
   print "program is half way"

Here is the output for the start_time :

19:00
19:00
19:00
19:00
19:00
19:00
19:00

Here is the output for the current_time :

00:10:36 T:5304  NOTICE: 00:09

Here is the output for the stop_time :

00:09:33 T:6824  NOTICE: 19:30
00:09:33 T:6824  NOTICE: 20:00
00:09:33 T:6824  NOTICE: 19:30
00:09:33 T:6824  NOTICE: 20:00
00:09:33 T:6824  NOTICE: 20:00
00:09:33 T:6824  NOTICE: 19:30
00:09:33 T:6824  NOTICE: 20:00

When I try this:

if start_time <=> current_time < stop_time:
   print "program has finished"

It will give me an error: invalid syntax

It won't let me to have the = in the statement, I can only write on the statement with less than or greater than but not with the = .

How I can include the equal = with less than and greater than for the start_time to compare the time with current_time ?

EDIT: Opps, sorry my mistake. I pasted the wrong code so here is what I am trying to do:

if start_time <=> current_time < stop_time:
    print "program is half way"

From what I understand, you want to check that both of these hold:

  1. current_time is either less than, greater than, or equal to start_time .
  2. current_time is less than stop_time .

Now, the first one can be written like this:

if current_time < start_time or current_time > start_time or current_time == start_time:
   print "program has finished"

However, this is always true because start_time and current_time will either be equal (making current_time == start_time true) or different (making current_time < start_time or current_time > start_time true).

The second one can be written like this:

if current_time < stop_time:
   print "program has finished"

So, since the first one is not needed, you just need:

if current_time < stop_time:
   print "program has finished"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM