需要帮助/反馈在python中添加两次

问题描述 投票:-1回答:2

我目前正在研究有关时钟算术的程序。我的程序要求用户分两次,标准格式和军事时间[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]格式的方法。我的程序代码如下所示。非常感谢,

Code

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)

What the console shows:

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
python arithmetic-expressions
2个回答
1
投票

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

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)))

0
投票

我想我们可以使用一些内置的日期时间函数,而不是自己计算它们:

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的输出是:

page_22:00:02

© www.soinside.com 2019 - 2024. All rights reserved.