不知道如何在一个国际单项体育联合会成员国内作出或陈述

2024-10-01 15:36:40 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使if语句具有两个同义词值,即“display”和“screen”,然后是一个and和另一个字符串,如“breake”。其思想是,只有在出现“display”和“breaked”或“screen”和“breaked”时,它才会输出

我尝试过:

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")

Tags: and字符串inifdefdisplayissue语句
1条回答
网友
1楼 · 发布于 2024-10-01 15:36:40

问题是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:

现在它这样说:“如果显示器或屏幕有问题,条件成立破碎也是一个问题。如果没有括号,它会说: “如果显示有问题,则条件成立屏幕和断屏均在问题中

相关问题 更多 >

    热门问题