如何使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")
答案 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:
现在它说:" 如果显示或屏幕有问题,条件成立; 和损坏也是问题"。没有括号,它会说: " 如果显示有问题,条件成立; 或屏幕和已损坏的问题"。