0

我正在编写这个 Python 在线课程介绍。我接近这个问题的解决方案,内容如下:

这个程序需要两行输入。第一行是以 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。

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

4

9 回答 9

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")
于 2013-09-05T17:26:36.107 回答
1

请注意,当 D > 60 时,例如您的示例损坏案例,您永远不会检查 if M + (D % 60) > 60,因此您会得到 23:01 的错误答案。

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

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

所以如果D < 60, then(D // 60)将为零,如果(M + (D % 60)) < 60, then((M + (D % 60)) // 60)也将为零。如果等于 24,第二行将finalhr变为 0,如果等于 25,则变为 1,依此类推。这应该给你正确的小时数。

于 2013-09-05T17:53:00.183 回答
0
H, M = map(int, ST.split(":"))
m = (M + int(D)) % 60
h = (H + (M + int(D)) // 60) % 24
print "%i:%i"%(h, m)
于 2013-09-05T17:29:05.190 回答
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
于 2014-06-26T21:27:52.893 回答
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))

干杯.kg

于 2015-05-11T13:53:21.283 回答
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)
于 2016-09-09T21:19:23.223 回答
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))
于 2017-03-08T22:12:57.697 回答
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
于 2017-03-24T12:47:28.380 回答
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))

于 2017-12-19T22:28:20.673 回答