import datetimeimport randomt = datetime.datetime.now()print(t)if(t.hour == random.randint(0,11)): print("Good Morning") else: print("Good Evening")
2 回答

郎朗坤
TA貢獻1921條經驗 獲得超9個贊
您將當前小時與 0 到 11 之間隨機選擇的小時相匹配,因此您可能找不到匹配項。如果你想測試當前小時是否在 0 到 11 之間,你應該簡單地使用鏈式比較操作:
if 0 <= t.hour <= 11: print("Good Morning")
添加回答
舉報
0/150
提交
取消