python - “不在”运算符不采用“或”运算符
问题描述
我有这个小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 b
与not 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!!")
此帖子还建议使用以下链接。这也很好。所以我把它分享给那些将从中受益的人
解决方案
查看 的值a or b
:
>>> a = "cinema"
>>> b = "tennis"
>>> a or b
'cinema'
“或”运算符在属于in
.
推荐阅读
- node.js - Websockets 与针对用户特定细节的长轮询
- css - CSS – 边框底部的过渡不起作用
- r - 使用 glmmPQL (MASS) 和 stargazer/texreg/pander 制作漂亮的表格
- javascript - CSS rotateY 更新 img src 在转换完成之前不可见
- python - Django-如何“合并”两个序列化程序
- php - imagick 安装错误权限被拒绝
- foreach - xdocument 从头开始创建
- r - 将计算列添加到数据框 R
- go - Goland 上的 Golang 自动重载(Jetbrains IDE)?
- c# - 打开 xml sdk 2.0 使用公式读取 excel 单元格值