需要帮助/反馈才能在python中相加两次

时间:2018-09-13 03:45:21

标签: python arithmetic-expressions

我目前正在开发有关时钟算术的程序。我的程序要求用户使用标准格式和军事时间[HH:MM:SS]两次分开的时间。然后,我将这两次相加得到最终结果。我在尝试解决的两个问题上需要帮助,但我一直在努力解决。

最后,应该看起来像这样: HH:MM:SS + HH:MM:SS = HH:MM:SS 结果全取决于用户输入的内容

  1. 您能告诉我一种可以防止时间流逝/超过24小时,60分钟和60秒的方法吗?这些是临界值,如果小时,分钟或秒数超出了它们的临界值,那么总的时间就没有意义。我想知道我该怎么做,我想这将需要整数除法或模数运算符(%)。非常感谢您的帮助。我需要将时间保持在这些范围内,因为用户可以选择任何可能溢出的时间。在达到这些临界值之后,时间应重新回到零并从那里开始回绕。

  2. 如何确保我的最后时间保持[HH:MM:SS]格式?非常感谢您的帮助。有时,格式显示为[H:M:S],这是我不想要的。

非常感谢您为我所困扰的这两个问题提供帮助。我是如此接近一切正常。我只需要知道一些代码即可,可以在达到一定限制后中断时间重新启动,以及保持[HH:MM:SS]格式不变的方式。我的程序代码如下。非常感谢你,

代码

ClockTime1 = input('Enter clock two timie (in military time) in 
the format HH:MM:SS , it has to be in this format in order to 
function correctly :')
ClockTime2= input('Enter clock one time (in military time) in 
the format HH:MM:SS , it has to be in this format in order to 
function correctly :')
print(ClockTime1.split(':'))
print(ClockTime2.split(':'))
ClockTime1Hours= int((ClockTime1.split(':')[0]))
ClockTime2Hours= int((ClockTime2.split(':')[0]))
ClockTime2Minutes= int((ClockTime2.split(':')[1]))
ClockTime1Seconds= int((ClockTime1.split(':')[2]))
ClockTime2Seconds= int((ClockTime2.split(':')[2]))
print(ClockTime1Hours,'hours for clock 1')
print(ClockTime2Hours,'hours for clock 2')
print(ClockTime1Minutes,'minutes for clock 1')
print(ClockTime2Minutes,'minutes for clock 2')
print(ClockTime1Seconds,'seconds for clock 1')
print(ClockTime2Seconds,'seconds for clock 2')
ClockTime1Hours += ClockTime2Hours
print('sum of clock hours=',ClockTime1Hours)
ClockTime1Minutes += ClockTime2Minutes
print('sum of clock minutes=',ClockTime1Minutes)
ClockTime1Seconds += ClockTime2Seconds
print('sum of clock seconds=',ClockTime1Seconds)

控制台显示的内容:

Enter clock two time (in military time) in the format HH:MM:SS 
, it has to be in this format in order to function correctly 
:2:00:00

Enter clock one time (in military time) in the format HH:MM:SS 
, it has to be in this format in order to function correctly 
:3:00:00
['2', '00', '00']
['3', '00', '00']
2 hours for clock 1
3 hours for clock 2
0 minutes for clock 1
0 minutes for clock 2
0 seconds for clock 1
0 seconds for clock 2
sum of clock hours= 5
sum of clock minutes= 0
sum of clock seconds= 0
Sum of Times=5:0:0

2 个答案:

答案 0 :(得分:1)

我认为python时间模块可以提供帮助

import time

a = "00:00:00 1971"
a = time.mktime(time.strptime(a,"%H:%M:%S %Y"))
ClockTime1 = input('Enter clock two timie (in military time) in the format HH:MM:SS , 
it has to be in this format in order to function correctly :')
ClockTime2= input('Enter clock one time (in military time) in the format HH:MM:SS , 
it has to be in this format in order to function correctly :')
ClockTime1+=" 1971"
ClockTime2+=" 1971"
ClockTime1 = time.mktime(time.strptime(ClockTime1,"%H:%M:%S %Y"))
ClockTime2 = time.mktime(time.strptime(ClockTime2,"%H:%M:%S %Y"))
print(time.strftime("%H:%M:%S", time.localtime(ClockTime1-a+ClockTime2-a+a)))

答案 1 :(得分:0)

我认为我们可以使用一些内置的datetime函数来代替自己计算它们:

from datetime import datetime,timedelta
str_t1="23:00:01"
str_t2="23:00:01"
dt1 = datetime.strptime(str_t1, '%H:%M:%S')
dt2 = datetime.strptime(str_t2, '%H:%M:%S')
dt2_delta=timedelta(hours=dt2.hour, minutes=dt2.minute, seconds=dt2.second)
dt3=dt1+dt2_delta
str_t3=datetime.strftime(dt3,'%H:%M:%S')

str_t3的输出是:

  
    
      

str_t3       '22:00:02'