Python Pyschools主题3第9题
我是编程新手,我在Pyschools上试过这个问题 有人能帮我吗Python Pyschools主题3第9题,python,conditional,Python,Conditional,我是编程新手,我在Pyschools上试过这个问题 有人能帮我吗 编写一个函数,将时间转换为24小时格式。 例子 这个问题是有条件的 从末尾删除“am”或“pm”,并将其保存在某个地方 在“:”上拆分,这样就可以将小时和分钟分开 如果时间为“am”,则打印小时,除非小时为“12”,在这种情况下打印“00” 否则(如果时间为“pm”),打印小时数+12,除非小时数为“12”,在这种情况下,打印“12” 打印会议记录 def时间24小时(tstr): 时间=tstr.replace(':','')
编写一个函数,将时间转换为24小时格式。 例子 这个问题是有条件的
def时间24小时(tstr):
时间=tstr.replace(':','')
如果“am”及时:
tim=int(time.replace('am','')
如果“pm”及时到达:
tim=int(time.replace('pm','')
mini=tim%100
小时=tim//100
如果最小def时间为24小时(tstr):
time_list=tstr[:-2]。拆分(“:”)
time_int=[int(x)表示时间列表中的x]
am_pm=tstr[-2:]
小时=时间整[0]
分钟=时间[1]
如果am_pm==“am”:
如果小时数小于12:
小时=小时+12
返回“%02d%02dhr”%(小时,分钟)
其他:
返回“00%2小时”%(分钟)
其他:
如果小时数小于12:
小时=小时+12
返回“%02d%02dhr”%(小时,分钟)
其他:
返回“%02d%02dhr”%(小时,分钟)
对我来说,它很管用(但不太漂亮:'C):
def时间24小时(tstr):
a=列表(tstr)
如果len(a)==6:
a、 插入(0,'0')
如果a[5]=“a”和a[0]=“1”和a[1]=“2”:
dela[2]
德拉[4:6]
a、 附加('h')
a、 附加('r')
a[0]=“0”
a[1]=“0”
w=''。加入(a)
返回w
elif a[5]=“p”:
dela[2]
德拉[4:6]
a、 附加('h')
a、 附加('r')
x=a[0]+a[1]
打印x
y=int(x)
打印y
如果y注释内联
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
这对我更有效。我根据公认的答案对此进行了改进
我没有使用循环将列表转换为整数,而是调用实际列表中的值,并将变量声明为int类型。简单李>
我仍然认为没有理由在时间为上午12点且小于12小时时添加小时数+12–12小时和24小时的时间格式与上午12点的时间格式相同,但上午12点除外。因此,我只是从函数中返回早晨的初始值,当它正好是12时,我返回小时数%12以获得00 am
def时间24小时(tstr):
time_list=tstr[:-2]。拆分(“:”)
t_list_int=[]
对于e in time_列表:
t_list_int.append(e)
分钟=int(时间列表[1])
小时=整数(时间列表[0])
am_pm=tstr[-2:]
如果am_pm==“am”:
如果小时=12:
返回“%02d%02dhr”%(小时%12,分钟)
elif hours,也许这就是这个答案的代码“编写一个将时间转换为24小时格式的函数”>>>time24hr('12:34am')'0034hr'>>>time24hr('12:15pm')'1215hr'
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
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'
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)
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"
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
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