Python如何实现时间累加的计算器
1.输入一个时间(确保时间格式的正确)
2.判断是否是第一次,如果是第一次就和00:00:00进行累加;如果是就和上一次计算出来的时间进行累加;
3.如果秒数大于60就进1,分数大于60就进1
4.将操作记录追加写入到同级目录下的log.txt文件中
5.使用pyinstaller将文件打包成exe可执行文件(windows)
实例
import datetime
def time_split(t):
t = str(t).split(":")
if len(t) == 2:
t_h = 0
t_m = t[0]
t_s = t[1]
if len(t) == 3:
t_h = t[0]
t_m = t[1]
t_s = t[2]
return t_h, t_m, t_s
def time_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
while t3_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
while t3_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)
return t3
if __name__ == "__main__":
flag = True
index = 0
print(u"欢迎使用时间相加计算!")
print(u"请注意您的时间格式,且确保你的输入法是英文输入 ==> 时:分:秒 ==> 00:00:00")
print(u"将会在同级目录下生产log.txt文件来记录你的输入")
print(u"中文输入将会导致程序闪退!!!")
while flag:
time_in = input(u"请输入你要计算的时间(请确保你的“:”是英文字符!!!,输入“#”结束):")
with open('log.txt', mode='a') as filename:
filename.write(str(datetime.datetime.now()))
filename.write(u"请输入你要计算的时间(请确保你的“:”是英文字符!!!,输入“#”结束):" + time_in + u"\n")
filename.close()
if time_in == "#":
flag = False
print(u"计算结束,结果为:", time_last)
with open('log.txt', mode='a') as filename:
filename.write(str(datetime.datetime.now()))
filename.write(u"计算结束,结果为:" + time_last + u"\n")
filename.close()
break
if index == 0:
time_last = time_add(time_in, "00:00:00")
else:
time_last = time_add(time_in, time_last)
print("第", index+1 , "次合计:", time_last)
with open('log.txt', mode='a') as filename:
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电脑。
以上是 Python如何实现时间累加的计算器 的全部内容, 来源链接: utcz.com/z/544688.html