不知道如何在if语句中创建or和语句

时间:2017-01-29 17:58:37

标签: python python-3.x

如何使if语句具有两个同义词值,即" display"和"屏幕"然后是and和另一个字符串,例如"破坏"。我们的想法是,如果有一个"显示"它只会输出。和#34;破碎"或"屏幕"和"破碎"。

我尝试过:

def issuesection():
    issue = input("Type in your issue in a sentence and we will try out best to help you with a solution:  ")
    if "display" or "screen" in issue and "broken" in issue:
        print('WORKED')
    else:
        print("FAIL")

1 个答案:

答案 0 :(得分:1)

问题是Python看到了:

"display" or "screen" in issue

为:

("display") or ("screen" in issue)

因此,它会评估"display"真实性会将每个非空字符串视为True

所以你应该把它重写为:

if "display" in issue or "screen" in issue and "broken" in issue:

此外,由于您希望and绑定到in两个检查,因此您还应将括号作为and的左操作数:

if ("display" in issue or "screen" in issue) and "broken" in issue:

现在它说:" 如果显示或屏幕有问题,条件成立; 损坏也是问题"。没有括号,它会说:  " 如果显示有问题,条件成立; 屏幕和已损坏的问题"。