我是编程的新手,我正在Pyschools上尝试这个问题。
任何人都可以帮助我吗?
编写一个将时间转换为24小时格式的函数。 实例
>>> time24hr('12:34am')
'0034hr'
>>> time24hr('12:15pm')
'1215hr'
这个问题属于条件限制。
答案 0 :(得分:4)
答案 1 :(得分:0)
def time24hr(tstr):
time_list = tstr[:-2].split(':')
time_int = [int(x) for x in time_list]
am_pm = tstr[-2:]
hours = time_int[0]
minutes = time_int[1]
if am_pm == 'am':
if hours < 12:
hours = hours + 12
return "%02d%02dhr" % (hours,minutes)
else:
return "00%2dhr" % (minutes)
else:
if hours < 12:
hours = hours + 12
return "%02d%02dhr" % (hours,minutes)
else:
return "%02d%02dhr" % (hours,minutes)
答案 2 :(得分:0)
def time24hr(tstr):
newTime = ""
if "am" in tstr:
for i in tstr:
if i not in ":am":
newTime += i
if "12" in newTime[:2]:
newTime = newTime.replace("12", "00")
return newTime + "hr"
elif len(newTime) == 3:
newTime = "0" + newTime + "hr"
return newTime
elif "pm" in tstr:
for i in tstr:
if i not in "pm":
newTime += i
if "10" in newTime[:2]:
newTime = newTime.replace("10", "22")
newTime = newTime.replace(":", "")
elif "11" in newTime[:2]:
newTime = newTime.replace("11", "23")
newTime = newTime.replace(":", "")
elif "12" in newTime[:2]:
newTime = newTime.replace(":", "")
elif "1" in newTime[:1]:
newTime = newTime.replace("1", "13")
newTime = newTime.replace(":", "")
elif "2" in newTime[:1]:
newTime = newTime.replace("2", "14")
newTime = newTime.replace(":", "")
elif "3" in newTime[:1]:
newTime = newTime.replace("3", "15")
newTime = newTime.replace(":", "")
elif "4" in newTime[:1]:
newTime = newTime.replace("4", "16")
newTime = newTime.replace(":", "")
elif "5" in newTime[:1]:
newTime = newTime.replace("5", "17")
newTime = newTime.replace(":", "")
elif "6" in newTime[:1]:
newTime = newTime.replace("6", "18")
newTime = newTime.replace(":", "")
elif "7" in newTime[:1]:
newTime = newTime.replace("7", "19")
newTime = newTime.replace(":", "")
elif "8" in newTime[:1]:
newTime = newTime.replace("8", "20")
newTime = newTime.replace(":", "")
elif "9" in newTime[:1]:
newTime = newTime.replace("9", "21")
newTime = newTime.replace(":", "")
return newTime + "hr"
答案 3 :(得分:0)
对我来说,它可以工作(但不太漂亮:'C):
def time24hr(tstr):
a=list(tstr)
if len(a)==6:
a.insert(0,'0')
if a[5]=='a' and a[0]=='1' and a[1]=='2':
del a[2]
del a[4:6]
a.append('h')
a.append('r')
a[0]='0'
a[1]='0'
w=''.join(a)
return w
elif a[5]=='p':
del a[2]
del a[4:6]
a.append('h')
a.append('r')
x=a[0]+a[1]
print x
y=int(x)
print y
if y<12:
z=y+12
g=str(z)
a[0:2]=g
w=''.join(a)
return w
else:
w=''.join(a)
return w
else:
del a[2]
del a[4:6]
a.append('h')
a.append('r')
w=''.join(a)
return w
答案 4 :(得分:0)
内联评论
def time24hr(s):
c = s.split(':') # Split string to list ["hh", "mmAP"] AP is "am" or "pm"
h = int(c[0]) # convert hours string to integer
m = int(c[1][0:-2]) # remove "AP" from "mmAP" and convert to integer. This will handle "mAP" input correctly as well.
h = h % 12 if s[-2:] != 'pm' else 12 if h == 12 else h + 12 # convert hours to 24hr format, handling edge cases.
return "%02d%02dhr" % (h, m) # Print hours and minutes as %02d - two digits, potentially filling with zeroes
答案 5 :(得分:0)
这对我来说更好。我根据接受的答案对此进行了改进。
def time24hr(tstr):
time_list = tstr[:-2].split(':')
t_list_int = []
for e in time_list:
t_list_int.append(e)
minutes = int(time_list[1])
hours = int(time_list[0])
am_pm = tstr[-2:]
if am_pm == 'am':
if hours == 12:
return "%02d%02dhr" % (hours%12,minutes)
elif hours <12:
return "%02d%02dhr" %(hours, minutes)
else:
if hours == 12:
return "%02d%02dhr" %(hours, minutes)
elif hours <12:
return "%02d%02dhr" %(hours+12, minutes)
print(time24hr('9:34pm'))
print(time24hr('12:00am'))
答案 6 :(得分:-1)
def time24hr(tstr):
time=tstr.replace(':','')
if 'am' in time:
tim=int(time.replace('am',''))
elif 'pm' in time:
tim=int(time.replace('pm',''))
mini=tim%100
hour=tim//100
if mini<10:
mini='0'+str(mini)
else:
mini=str(mini)
if 'am' in time:
if hour<10:
hour='0'+str(hour)
elif hour==12:
hour='0'+str(hour-12)
time1=time.replace('am','')
time1=str(hour)+str(mini)+'hr'
elif 'pm' in time:
if hour<12:
hour=12+hour
elif hour==12:
hour=hour
time1=time.replace('pm','')
time1=str(hour)+str(mini)+'hr'
return time1
答案 7 :(得分:-1)
def time24hr(tstr):
a = tstr
b = int(a[:2])+12
if a[-2:] =='am' and a[:2]=='12':
return '\'00'+a[3:-2]+'hr\''
elif a[-2:]=='pm' and a[:2]=='12':
return '\'12'+a[3:-2]+'hr\''
else:
if a[-2:] =='pm':
return "\'"+str(b)+a[3:-2]+'hr\''
else:
return "'"+a[:2]+a[3:-2]+'hr\''
print time24hr('12:15pm')
# '1215hr'
print time24hr('12:15pm')
print time24hr('12:34am')
# '0034hr'
print time24hr('08:34am')
# '0834hr'
print time24hr('08:34pm')
'2034hr'