Python - 具有前导零的24小时时钟逻辑的结束时间

时间:2013-09-05 17:16:33

标签: python datetime

我正在研究this intro to python online course。我接近解决这个问题的解决方案:

  

该程序需要两行输入。第一行是“开始时间”,以24小时制表示,带有前导零,如08:30或14:07。第二行是以分钟为单位的持续时间D.打印出开始时间后D分钟的时间。例如,输入

12:30
47

输出:

13:17

提示:

Break the input into hours and minutes.
Final minutes should be (M + D) % 60.
Final hours requires //60 and % 24.

到目前为止我的解决方案:

ST = input()
STlen = len(ST)
D = int(input())

for position in range (0,STlen):
   if ST[position] == ':':
      H = int(ST[0:position])
      M = int(ST[position+1:STlen])

#minutes section
final_min = (M+D) % 60
if final_min < 10:
   finalminstr1 = str(final_min)
   zeroed_finalmin_str = '0' + finalminstr1
   final_min = zeroed_finalmin_str
else:
   final_min = (M+D) % 60

#hours section
if D > 60:
   finalhr = (D // 60) + H
elif (M+D) % 60 > 0 and H % 24 == H and D < 60:
   finalhr = H+1
if finalhr == 24:
   finalhr = '00'

#getting final end time ready
finalminstr = str(final_min)
finalhrstr = str(finalhr)
endtime = finalhrstr + ":" + finalminstr
print(endtime)

我认为我的方法过于复杂,会浪费处理时间。当我使用

时,它也会破坏
15:33
508

作为输入数据。正确的输出应该是00:01,但我得到了23:01。

有关如何改进代码的任何想法? 另外,请不要使用函数或方法。我们还没有学习如何使用函数或方法!

9 个答案:

答案 0 :(得分:4)

H,M = map(int,"15:33".split(":"))
D = int("50")
M_new = M + D
H_new = H + (M_new // 60)
M_new = M_new % 60
EXTRA_DAYS = H_new // 24
H_new = H_new % 24 # in case you go over 24
print "%02d:%02d%s"%(H_new,M_new,"" if not EXTRA_DAYS else " +%dD"%EXTRA_DAYS)

虽然这真的是日期时间的一个案例(以下是你在现实世界中的表现)

import datetime
my_date = datetime.datetime.strptime("13:55","%H:%M")
time_delta = datetime.timedelta(minutes=50)
print (my_date + time_delta).strftime("%H:%M")

答案 1 :(得分:1)

注意当D> 60,例如你的例子破案,你永远不会检查M + (D % 60) > 60,所以你留下了23:01的错误答案。

我认为你实际需要检查的是M + (D % 60) > 60,这意味着你必须将H增加一个,然后检查是否将它推到了24以上。这样做的一种简单方法只是改变你的if结构,我不习惯python,但看起来elif意味着else if正确吗?如果是这样,你可以重组该块看起来像这样:

finalhr = (D // 60) + H + ((M + (D % 60)) // 60)
finalhr = finalhr % 24

因此,如果D < 60,那么(D // 60)将为零,如果(M + (D % 60)) < 60,则((M + (D % 60)) // 60)也将为零。如果等于24,则第二行将使finalhr为零,如果等于25,则为{1},依此类推。这应该给你正确的小时数。

答案 2 :(得分:0)

H, M = map(int, ST.split(":"))
m = (M + int(D)) % 60
h = (H + (M + int(D)) // 60) % 24
print "%i:%i"%(h, m)

答案 3 :(得分:0)

这对我有用。它包括在单个数字前面的额外零,看起来像其他人没有包括。但是,我也是初学者,所以它可能比需要更长/更复杂......但它确实有效。 :)

time = input()
dur = int(input())
x=time.find(':')
hours = int(time[0:x]) 
minutes = int(time[x+1:len(time)])
newmin = minutes+dur

if (newmin > 59):
   plushrs = int(newmin//60) 
   newmin = newmin % 60
   hours = hours+plushrs
if (hours >23):
   while hours > 23: #incase they add more than a whole day
     hours = hours - 24
   if (hours <10): #adding the zero for single digit hours
     hours = '0' + str(hours)
else:
   hours = str(hours)
if newmin > 9:
   newmin = str(newmin)
   print (hours + ':' + newmin) #print out for double digit mins
else:
   newmin = str(newmin)
   print (hours + ':0' + newmin) #print out for single digit mins

答案 4 :(得分:0)

我已经用这种方式解决了 - 这被标记为正确。我想有更优雅的方式,但这是我想到的方式: - )

Lesson 8.5 - Ending Time

t = input()
p = input()
pos = 0

for myString in range(0, (len(t))):
    # print(S[myString])
    if t[myString] == ":":
        pos = myString
        # print(pos)
hh = t[0:pos]
mm = t[pos+1:len(t)]

hh = int(hh)
mm = int(mm)
p = int(p)
if (mm + p) > 59:
    hh = hh + (mm + p) // 60
    if (hh > 23):
        hh = hh - 24
    mm = (mm + p) %  60
else:
    mm = mm + p
print('{num:02d}'.format(num=hh) + ":" + '{num:02d}'.format(num=mm))

Cheers.kg

答案 5 :(得分:0)

这是我的回答

# Input time expressed as 24-clock with leading zeroes

# Starting time
Stime = input("Input starting time in format like 12:15. ")

# Duration in minutes
Duration = int(input("Input duration in minutes. "))

# Extracting integer numbers of 'hours' and 'minutes'
hour = int(Stime[0:2])
minute = int(Stime[3:5])

# Getting new 'hours' and 'minutes'
NewHour = ((hour + ((minute + Duration) // 60)) % 24)
NewMinute = (minute + Duration) % 60
if NewHour < 10:
NewHour = '0' + str(NewHour)
else:
    NewHour = str(NewHour)
if NewMinute < 10:
    NewMinute = '0' + str(NewMinute)
else:
    NewMinute = str(NewMinute)

# Print new time
print("New time is " + NewHour + ":" + NewMinute)

答案 6 :(得分:0)

#receive user input
time=input()
dur=int(input())

#separate hours and minutes
hrs=time[0:time.find(':')]
mins=time[time.find(':')+1:len(time)]

#take care of minute calculation
newmins=(int(mins)+dur)%60

#take care of hour calculation
hrstoadd=(int(mins)+dur)//60
newhr=(int(hrs)+hrstoadd)%24

#add zeros to hour and min if between 0-9
if newhr < 10:
   newhr='0'+str(newhr)
if newmins < 10:
   newmins='0'+str(newmins)

#print new time
print(str(newhr)+':'+str(newmins))

答案 7 :(得分:0)

贝娄是我的回答: 我将时间分为两个部分,一个小时,一个几分钟,然后分别计算:

hour = input() #input from the user
minute = int(input())
#split the time string in hours and minutes
for position in range(0, len(hour)):
   if hour[position] == ":":
      h = int(hour[0:position])
      min = int(hour[position+1:len(hour)])
#below I have split the final answer in 2 parts for easier calculation
x = (min + minute)//60 #the number of hours 
y = (min + minute) - (x *60) # the final number of minutes
h1 = (h + ((min+minute)//60))%24 # the final number of hours that will be printed
p1s = h1//10 # we are calculating the first digit of the hour part
p2s = h1%10 # we are calculating the second digit of the hour part
p1d = y // 10 # we are calculating the first digit of the minutes part
p2d = y % 10 10 # we are calculating the second digit of the minutes part
s = str(p1s)+str(p2s) # we are converting them to strings (right part and the left obe)
d = str(p1d)+str(p2d)
if s == "24": # if the hour part is 24 we assign the correct value
   s = "00"
f = s+":"+d # a final concatenate
print(f) # and here we are

答案 8 :(得分:0)

#My VARIANT
HM = input()
D = int(input())
h=int(HM[0:2])
MM=int(HM[3:5])
m = (MM+D)%60
hh = (MM+D)//60
h=(h+hh)%24
if len(str(m))==1:
   m='0'+str(m)
if len(str(h))==1:
   h='0'+str(h)
print(str(h) + ':' + str(m))