首页 > 解决方案 > “不在”运算符不采用“或”运算符

问题描述

我有这个小python代码,它不像下面那样工作。

while True:
    activity = input("What would you like to do today? ")
    a="cinema"
    b="tennis"
    if a or b not in activity.lower():
        print(f"Instead of {activity} I want to go to the cinema")
    else:
        print("Sure Let's do that!!")

但是,当我提取 时or b,它工作正常


while True:
    activity = input("What would you like to do today? ")
    a="cinema"
    b="tennis"
    if a  not in activity.lower():
        print(f"Instead of {activity} I want to go to the cinema")
    else:
        print("Sure Let's do that!!")

问题是为什么不a or bnot in工作一起使用?

编辑:我已经完成了以下修改,但仍然无法正常工作。

while True:
    activity = input("What would you like to do today? ")
    a="cinema"
    b="tennis"
    if (a not in activity.lower()) or (b not in activity.lower()):
        print(f"Instead of {activity} I want to go to the cinema")
    else:
        print("Sure Let's do that!!")

此帖子还建议使用以下链接。这也很好。所以我把它分享给那些将从中受益的人

如何针对一个值测试多个变量?

标签: pythonpython-3.x

解决方案


查看 的值a or b

>>> a = "cinema"
>>> b = "tennis"
>>> a or b
'cinema'

“或”运算符在属于in.


推荐阅读