1.输入一段时间(确保正确的时间格式)
2.判断是否是第一次,如果是第一次,就是00:00:00累加;如果与上次计算的时间累加;
3.如果秒数大于60,则进入1,如果分数大于60,则进入1
4.将操作记录添加到同级目录中.txt文件中
5.用pyinstaller将文件打包成exe可执行文件(windows)
实例
importdatetime deftime_split(t): t=str(t).split(":") iflen(t)==2: t_h=0 t_m=t[0] t_s=t[1] iflen(t)==3: t_h=t[0] t_m=t[1] t_s=t[2] returnt_h,t_m,t_s deftime_add(t1,t2): t1_h=time_split(t1)[0] t1_m=time_split(t1)[1] t1_s=time_split(t1)[2] t2_h=time_split(t2)[0] t2_m=time_split(t2)[1] t2_s=time_split(t2)[2] t3_s=int(t1_s)+int(t2_s) t3_sup=0 whilet3_____s>=60: t3_sup+=1 t3_s=int(t3_s)-60 t3_m=int(t1_m)+int(t2_m)+int(t3_sup) t3_mup=0 whilet3_____m>=60: t3_mup+=1 t3_m=int(t3_m)-60 t3_h=int(t1_h)+int(t2_h)+int(t3_mup) t3=str(t3_h).zfill(2)+":"+str(t3_m).zfill(2)+":"+str(t3_s).zfill(2) returnt3 if__name__=="__main__": flag=True index=0 print(u"欢迎计算使用时间!") print(u"请注意您的时间格式,并确保您的输入法是英文输入==>时:分:秒==>00:00:00") print(u"log将在同级目录中生产.记录您的输入txt文件") print(u"中文输入会导致程序闪回!!!!") whileflag: time_in=input(u"请输入您要计算的时间(请确保您的":"是英文字符!!!,输入“#”结束):") withopen('log.txt',mode='a')asfilename: filename.write(str(datetime.datetime.now())) filename.write(u"请输入您要计算的时间(请确保您的“”:"是英文字符!!!!,输入“#”结束):"+time_in+u"\n") filename.close() iftime_in=="#": flag=False print(u"结果为:",time_last) withopen('log.txt',mode='a')asfilename: filename.write(str(datetime.datetime.now())) filename.write(u"计算结束,结果为:"+time_last+u"\n") filename.close() break ifindex==0: time_last=time_add(time_in,"00:00:00") else: time_last=time_add(time_in,time_last) print("第",index+1,"次合计:",time_last) withopen('log.txt',mode='a')asfilename: filename.write(str(datetime.datetime.now())) filename.write(u"第"+str(index+1)+u"次合计:"+time_last+u"\n") filename.close() index+=1
以上就是Python实现时间累积计算器的方法,希望对大家有所帮助。更多Python学习指导:基础教程python基础教程
推荐操作环境:windows7系统Python 3.9.1,DELL G3电脑。