from datetime import datetime as dt
while True:
try:
x = input().strip('\r')
test = dt.strptime(x, "%H %M")
s = dt.strptime("07 30", "%H %M")
e = dt.strptime("17 00", "%H %M")
if s <= test and test < e:
print('At School')
else:
print('Off School')
except EOFError:
break
就這樣
使用datetime的方法真高級,厲害!
下面是一些小想法:
if s <= test and test < e:
應該可以再更簡化
if s <= test < e:
就可以了
這是Python的特色之一,叫做Chained comparison
https://docs.python.org/zh-tw/3/reference/expressions.html#comparisons
https://youtu.be/M3GAJ1AIIlA