我是python的新手,试图解决这个问题:
输入为:
1.One integer for local time in Eastern time zone
2.One string containing “am” or “pm”.
3.All other variations such as Am, Pm.
4.AM, PM should result in a message stating that input format is not right
输出为:
___ EST is ___Central Time(CT)
___ EST is ___Mountain Time(MT)
___EST is___Pacific Time(PT)
以下是目前的代码:
#Function 1:
def time_in_24h(time,time_day):
''' (number, str)--> number
This function converts a 12-hour time, represented by am/pm,to its equivalent 24-hour time.
Returns time in the 24-hour format.
'''
if time_day!='pm' and time_day!='am':
print('Input is not in the right format') #this is where input is not in the right format
return 0
else: print
return # write the rest of the program here
#Function 2:
def time_in_12h(time_24):
''' (number)--> (number,str)
This function converts a 24-hour time to the equivalent 12-hour time.
Returns time in the 12-hour format with am/pm.
'''
if 12< time_24 <24:
return (time_24)-12,'pm'
elif: # write the rest of the program here, I am lost :(
#Function 3: Main function where an Eastern time zone is the input
def time_zone(time,am_pm):
time_24=time_in_24h(time,am_pm)
答案 0 :(得分:2)
time_in_24h(time,time_day)
此功能将12小时时间(以am / pm为单位)转换为等效的24小时时间。以24小时格式返回时间。
让我们通过查看示例来考虑这是如何工作的:
5 pm
是什么? “pm”的意思是“发布meridiem”或“中午之后”,所以这是中午后的小时。因此,“下午5点”的24小时格式为17小时,5 + 12
。12 + 12 = 24
,尽管是“我”。12
,尽管是“下午”。所以,你的代码看起来像这样:
if time == 12:
if time_day == 'am':
return …
else:
return …
elif time_day == 'pm':
return …
elif time_day == 'am':
return …
time_in_12h(time_24)
此功能可将24小时时间转换为相当于12小时的时间。以am / pm返回12小时格式的时间。
这与上面相同,只是相反。只需反转上面应用的逻辑,您就可以简单地填写函数的其余部分。特别是因为你的代码已经完成了一半的工作。