[英]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. 我想检查变量
start_time
是否小于,等于或大于current_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
: 这是
start_time
的输出:
19:00
19:00
19:00
19:00
19:00
19:00
19:00
Here is the output for the current_time
: 这是
current_time
的输出:
00:10:36 T:5304 NOTICE: 00:09
Here is the output for the stop_time
: 这是
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
这会给我一个错误:
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
? 如何包含等于和大于等于
start_time
的equal =
以将时间与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: 据我了解,您想检查一下两者是否成立:
current_time
is either less than, greater than, or equal to start_time
. current_time
小于,大于或等于start_time
。 current_time
is less than stop_time
. current_time
小于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). 但是,这总是正确的,因为
start_time
和current_time
将相等(使current_time == start_time
真)或不同(使current_time < start_time or current_time > start_time
真)。
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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.