Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 嵌套的If/Else控制流在不应执行时执行';t可能是逻辑操作错误_Python_String_If Statement_Logic - Fatal编程技术网

Python 嵌套的If/Else控制流在不应执行时执行';t可能是逻辑操作错误

Python 嵌套的If/Else控制流在不应执行时执行';t可能是逻辑操作错误,python,string,if-statement,logic,Python,String,If Statement,Logic,我有这个密码: 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: #

我有这个密码:

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."
因此,无论我键入
12:15am
12:15pm
,如果该代码在字符串中找到
12
,则开始执行上述代码<代码>打印时间24小时('12:15pm')返回
0015pm
,但应返回
0015hr
,且仅适用于包含
am
的字符串。否则,不要将
12
更改为
00
并返回
12:44pm
1244hr

我的问题是,为什么tstr:中的逻辑检查
if('a'和':')和tstr:
中的
elif('p'和':')不起作用?
此代码是此测验的解决方案->

==================================================================================

感谢您在逻辑操作方面对我的帮助

另外,我已经完成了上述测验,下面是工作代码:

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."
def时间24小时(tstr):
如果(len(tstr[:tstr.find(':')))==2)和(tstr[0]==0'):
tstr=tstr.更换(tstr[0],“”)
如果tstr中的('a')和tstr中的(':'):
如果tstr[:2]=“12”:
tstr=tstr.replace('12','00')。replace(':','')。replace('am','hr'))
返回tstr
elif len(tstr[:tstr.find(':')])<2:
#如果字符串的“tstr”切片(在字符串“:”之前结束)的长度小于2
tstr=tstr.replace(tstr[:tstr.find(':')],'0'+tstr[:tstr.find(':')))。replace(':','')。replace('am','hr'))
#将一个字符串替换为“0”和这一个字符串的组合。然后删除冒号,将其替换为缺少字符,并将“am”字符串替换为“hr”。
返回tstr
其他:
tstr=tstr.replace(':','').replace('am','hr'))
返回tstr
elif('p'在tstr中)和(':'在tstr中):
如果tstr[:2]=“12”:
tstr=tstr.replace(':','').replace('pm','hr'))
返回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'))
#将每个“数字”(字面上是字符串)替换为“PmDict”字典中对应的24小时格式“数字”。然后,与上述情况一样,删除冒号“:”,将其替换为缺少字符或空格,然后将“pm”替换为“hr”
返回tstr
其他:
return“时间格式未知。请键入正确的时间。”
正如你所看到的,我没有按照KISS规则编写这段代码,因为它有点复杂,但在我看来运行得相当好

可以在此处进行测试->

为大家干杯,感谢你们的帮助:)

if ':' in tstr:
希望这能让你洞察问题所在

可能替换为

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

if ':' in tstr:
希望这能让你洞察问题所在

可能替换为

if 'a' in tstr and ':' in tstr:
那么您的第一张支票:

('a' and ':') in tstr
只是检查“:”是否在tstr中。我不确定这到底是为什么,但如果在交互式Python中运行
('a'和':')
语句,您将看到它返回':'

你的第二张支票也有同样的错误

考虑使用

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

不幸的是,Python只能像这样英语:)

那么您的第一个检查:

('a' and ':') in tstr
只是检查“:”是否在tstr中。我不确定这到底是为什么,但如果在交互式Python中运行
('a'和':')
语句,您将看到它返回':'

你的第二张支票也有同样的错误

考虑使用

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


不幸的是,Python只能说得像英语:)

您的主要问题是检查子字符串包含的代码不正确:您在
中使用的
。当你写作时

if ('a' and ':') in tstr:
它首先计算
('a'和':')
以获得
,然后检查tstr中的
是否':'。这就是为什么第一个
if
块的计算结果一直为true。这是因为
是一个布尔运算,
x和y
相当于
x,如果bool(x)else y
。此表达式在您进入
检查中的
之前进行计算

你可以用

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

(对另一个检查也要这样做)

您的主要问题是对子字符串包含的代码检查不正确:您在
中使用了
。当你写作时

if ('a' and ':') in tstr:
它首先计算
('a'和':')
以获得
,然后检查tstr中的
是否':'。这就是为什么第一个
if
块的计算结果一直为true。这是因为
是一个布尔运算,
x和y
相当于
x,如果bool(x)else y
。此表达式在您进入
检查中的
之前进行计算

你可以用

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

(另一项检查也是如此)

您可以使用
all
对存在多个元素的测试进行概括:

if all(ch in tstr for ch in 'a:'):
    etc...

您可以使用
all
,对存在多个元素的测试进行概括:

if all(ch in tstr for ch in 'a:'):
    etc...

为什么
“a”和“:”
的计算结果是
?@arxanas——因为非空字符串是true。
操作符看着左边的对象,问道——这是真的吗?如果不是,则返回左对象。如果左对象为true like,则返回右对象。@arxanas类似地
'a'或':'
计算结果为
'a'
。如果这有效,则将其标记为答案,然后问题将标记为solvedOk,我想出了正确的想法并制定了解决方案。解决方案在上面的更新问题中。干杯为什么
“a”和“:”
的计算结果为
?@arxanas——因为非空字符串