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

问题描述 投票:0回答:9

我正在研究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。

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

python datetime
9个回答
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)也将为零。如果finalhr等于24,则第二行将使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) 为零,如果它等于25则为1,依此类推。这应该给你正确的小时数。


4
投票
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")

虽然这真的是日期时间的例子(继承人你将如何在真实世界中做到这一点)

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

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

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

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)

cheers.看过


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

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

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

0
投票

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