Skip to main content
 首页 » 编程设计

python之嵌套的 If/Else 控制流在不应该执行时执行之可能是逻辑操作错误

2025年05月04日18shihaiming

我有这个代码:

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' 和 ':') 在 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

大家干杯,感谢您的帮助:)

请您参考如下方法:

if ('a' and ':') in tstr: 

与

相同
if ':' in tstr: 

希望这能让您洞察问题所在。

可能替换为

if 'a' in tstr and ':' in tstr: