【发布时间】:2012-08-13 23:59:32
【问题描述】:
我有这个代码:
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。但是当我在其中输入带有12 的字符串时,它会自动省略这部分代码if ('a' and ':') in tstr: 或elif ('p' and ':') in tstr: 并继续到这部分:
if tstr[:2] == '12':
tstr = tstr.replace('12', '00').replace(':','').replace('am','hr')
return tstr
所以无论我输入12:15am 还是12:15pm,这段代码如果在字符串中找到12,就会开始执行上面的代码。 print time24hr('12:15pm') 返回 0015pm 但应该返回 0015hr 并且仅适用于其中包含 am 的字符串。否则,不要将12 更改为00 并返回,即1244hr 为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
大家干杯,感谢您的帮助:)
【问题讨论】:
-
因为这不是
and运算符的工作方式。它评估其操作数并返回确定最终结果的操作数。'a' and ':'是':',因为('a'和':')都计算为True。0 and 'foo'将导致0,因为0的计算结果为False。然后将结果传递给in运算符,因此您得到':' in tstr。 -
只是对@FelixKling 的评论稍作澄清:它不一定评估两个操作数。如果左操作数为真,它只评估右操作数。 (这被称为
short circuit evaluation)
标签: python string if-statement logic