我有这个代码:
def time24hr(tstr):
if ('a' and ':') in tstr:
if tstr[:2] == '12':
tstr = tstr.replace('12', '00').replace(':','').replace('am','hr')
return tstr
elif len(tstr[:tstr.find(':')]) < 2:
# If length of string's 'tstr' slice (that ends before string ':') is less than 2
tstr = tstr.replace(tstr[:tstr.find(':')],'0' + tstr[:tstr.find(':')]).replace(':', '').replace('am','hr')
# Replace one-character string with combination of '0' and this one-character string. Then remove colon, by replacing it with lack of characters and replace 'am' string with 'hr'.
return tstr
else:
tstr = tstr.replace(':', '').replace('am', 'hr')
return tstr
elif ('p' and ':') in tstr:
tstr = tstr.replace(':', '').replace('pm', 'hr')
return tstr
else:
return "Time format unknown. Type correct time."
当我执行此代码时,它应该print time24hr('12:34am')返回0034hr字符串。它也适用于此print time24hr('2:59am'),返回0259hr。但是当我在其中键入 string 时12,它会自动省略这部分代码if ('a' and ':') in tstr:或 thiselif ('p' and ':') in tstr:并继续执行此部分:
if tstr[:2] == '12':
tstr = tstr.replace('12', '00').replace(':','').replace('am','hr')
return tstr
因此,无论我输入12:15amor 12:15pm,此代码如果找到12in 字符串,就会开始执行上述代码。print time24hr('12:15pm')返回0015pm但应该返回0015hr,并且只针对其中包含的字符串am。否则,不要更改12为00并返回 ie 1244hrfor 12:44pm。
if ('a' and ':') in tstr:我的问题是,为什么那些逻辑检查elif ('p' and ':') in tstr:不起作用?此代码旨在成为此测验的解决方案-> http://www.pyschools.com/quiz/view_question/s3-q8
==================================================== =================================
感谢您帮助我进行逻辑运算。
另外,我已经完成了上面提到的测验,这是工作代码:
def time24hr(tstr):
if (len(tstr[:tstr.find(':')]) == 2) and (tstr[0] == '0'):
tstr = tstr.replace(tstr[0], '')
if ('a' in tstr) and (':' in tstr):
if tstr[:2] == '12':
tstr = tstr.replace('12', '00').replace(':', '').replace('am', 'hr')
return tstr
elif len(tstr[:tstr.find(':')]) < 2:
# If length of string's 'tstr' slice (that ends before string ':') is less than 2
tstr = tstr.replace(tstr[:tstr.find(':')], '0' + tstr[:tstr.find(':')]).replace(':', '').replace('am', 'hr')
# Replace one-character string with combination of '0' and this one-character string. Then remove colon, by replacing it with lack of characters and replace 'am' string with 'hr'.
return tstr
else:
tstr = tstr.replace(':', '').replace('am', 'hr')
return tstr
elif ('p' in tstr) and (':' in tstr):
if tstr[:2] == '12':
tstr = tstr.replace(':', '').replace('pm', 'hr')
return tstr
elif len(tstr[:tstr.find(':')]) < 2:
PmDict = {'0':'12','1':'13', '2':'14', '3':'15', '4':'16', '5':'17', '6':'18', '7':'19', '8':'20', '9':'21', '10':'22', '11':'23'}
tstr = tstr.replace(tstr[:tstr.find(':')], PmDict[tstr[:tstr.find(':')]]).replace(':', '').replace('pm', 'hr')
# Replace every "number" (which is string literally) with it's corresponding "number" in 24HR format, found in 'PmDict' dictionary. Then, as in above cases, remove colon ':' by replacing it with lack of character or space and then replace 'pm' with 'hr'
return tstr
else:
return "Time format unknown. Type correct time."
如您所见,我没有根据 KISS 规则编写此代码 - 因为它有点复杂,但在 IMO 中运行良好。
可以在这里测试-> http://doc.pyschools.com/console
为大家干杯,感谢您的帮助:)