用python做一个简易的闹钟
1、加载time模块,获取此时此刻的时间:import timet = time.localtime()print(t)时间是以字典的形式出现的。
2、从字典里面提取时间信息:now = time.strftime("%H %M", t).split(' ')now[0]代表小时;now[1]代表分钟。
3、用input输入你想要的时间信息:h = input('请输入闹钟的时与分,用空格分割:').split(' ')
4、用if语句来判断现在的时间是否是你输入的时间:if h[0] == now[0] and h[1] == now[1]: print("时间到!")
5、无声的提示,算不上闹钟,所以,我们酋篚靶高需要声音提示:if h[0] == now[0] and h[1] == now[1]: playsound('a.wav')
6、但是,上面代码,你输入的时间如果不是现在的时间,闹钟将不会提示,因为程序运行之后马上停止了。
7、用while语句吊着程序不要停止,直到闹钟响过之后:while True: t 屏顿幂垂= time.localtime() now = time.strftime("%H %M", t).split(' ') if h[0] == now[0] and h[1] == now[1]: print("时间到!") playsound('a.wav')好了,快去试一下吧。
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:95
阅读量:37
阅读量:50
阅读量:57
阅读量:62