检查执行if语句的时间

bab*_*eir -1 python

我正在练习Python,我想编写一个程序来检查当前时间,看看它是否与下午2:12匹配并说:its lunch time所以首先我想出了使用import time模块,但我不知道如何?

我的问题是我不知道如何使用时间模块.或者我使用正确的语法?

我的代码:

import time

bake=time()
if bake == '2:12':
    print ('its time to eating lunch')
else :
   print ('its not the time for eating lunch')
Run Code Online (Sandbox Code Playgroud)

Nik*_*ble 7

我建议使用datetime模块,在这种情况下更为实用(如polku建议的那样).然后,您将直接访问小时和分钟,以检查是否是午餐时间.

更多信息可以在这里找到: 如何在python中获取当前时间并分解为年,月,日,小时,分钟?

import datetime
now = datetime.datetime.now()

if now.hour == 14 and now.minute == 12:
    print ('its time to eating lunch')
else :
    print ('its not the time for eating lunch')
Run Code Online (Sandbox Code Playgroud)