我希望实现的是当时间介于xx和yy之间时运行指定的操作。
(使用24小时格式)
实施例
我什么时候尝试过:
self.data = 0
localtime = time.strftime("%H%M", time.localtime())
localtime = int(localtime)
if localtime >= 2330 and localtime < 615 and self.data != 1:
[..] //running certain action
self.data = 1
elif localtime >= 615 and localtime < 900 and self.data != 2:
[..] //running certain action
self.data = 2
elif localtime >= 900 and localtime < 2330 and self.data != 3:
[..] //running certain action
self.data = 3
正如您所看到的,我的代码唯一的问题是,localtime
不能高于而不是<23>而低于一次超过615,所以上。我得到的唯一其他想法是创建一个列出所有24小时的数组并指定某种行为......但是有没有其他方法可以达到我想要的效果?
答案 0 :(得分:1)
可以使用or
代替and
吗?
if (localtime >= 2330 or localtime < 615) and self.data != 1:
或
if localtime >= 615 and localtime < 900:
if self.data != 2
[..] //running certain action
self.data = 2
elif localtime >= 900 and localtime < 2330:
if self.data != 3
[..] //running certain action
self.data = 3
else:
if self.data != 1
[..] //running certain action
self.data = 1