嵌套的If/Else控制流正在执行时不应该 - 逻辑操作错误

spa*_*ffy 1 python string logic if-statement

我有这个代码:

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."
Run Code Online (Sandbox Code Playgroud)

当我执行此代码时,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
Run Code Online (Sandbox Code Playgroud)

所以无论我输入12:15am或者12:15pm,如果找到12in字符串,这段代码就会开始执行上面的代码.print time24hr('12:15pm')返回0015pm但应返回0015hr,仅适用于其中的字符串am.否则,不改变1200和IE浏览器返回1244hr12: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."
Run Code Online (Sandbox Code Playgroud)

我没有根据KISS规则编写这段代码,正如你所看到的那样 - 因为它有点复杂,但IMO工作得很好.

它可以在这里测试 - > http://doc.pyschools.com/console

欢呼大家,谢谢你的帮助:)

Vai*_*hra 6

if ('a' and ':') in tstr:
Run Code Online (Sandbox Code Playgroud)

和...一样

if ':' in tstr:
Run Code Online (Sandbox Code Playgroud)

希望这能让您深入了解似乎存在的问题.

可能取代

if 'a' in tstr and ':' in tstr:
Run Code Online (Sandbox Code Playgroud)