0

私が達成したいのは、時間がxxとyyの間にあるときに指定されたアクションを実行することです。

(24 時間形式を使用)

元。

  1. 時と分が23:30より高く、06 :15より低い場合、指定された action を実行します
  2. 時と分が06:15より高く、09:00 より低い場合、指定されたアクションを実行します
  3. 時と分が09:00より高く、23:30 より低い場合、指定された action を実行します

いつ試しましたか:

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

ご覧のとおり、私のコードの唯一の問題は、一度に2330より高く、615 より低くlocaltimeできないことです。私が得た他の唯一のアイデアは、24時間すべてがリストされた配列を作成し、そのように特定のアクションを指定することです...しかし、私が望むものを達成する他の方法はありますか?

4

1 に答える 1

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
于 2013-09-10T06:08:51.960 に答える