首页 文章

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

提问于
浏览
0

如何使 if 语句具有两个同义词,即"display"和"screen",然后是 and 和另一个字符串,如"broken" . 我们的想法是只有在有"display"和"broken"或"screen"和"broken"时才会输出 .

我试过的:

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 回答

  • 1

    问题是Python看到:

    "display" or "screen" in issue
    

    如:

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

    所以它评估了 "display"every non-empty string is considered to be 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:
    

    现在它说:“如果显示或屏幕出现问题,则条件成立; and 破坏也存在问题". Without the brackets it would state: "如果显示有问题,条件成立; or 屏幕和破损问题” .

相关问题