Python逻辑if和或反向

2024-04-24 13:32:43 发布

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

Level = 4
Name = "Mike"
Form = None
if Level == 5 or Name in ['James','Chris','Alex'] or (Name in ['John','Mike'] and Form):

上面的代码正是我想要它做的,但是我不知道如何做相反的事情:

例如

if Level != 5 and Name not in ['James','Chris','Alex'] and (Name not in ['John','Mike'] and Form):

尽我所能接近,但效果不一样。你知道吗


Tags: orand代码nameinformnoneif
2条回答

你的问题有点模糊,据我所知应该很简单

if not (Level == 5 or Name in ['James','Chris','Alex'] or (Name in ['John','Mike'] and Form)):

另一个简单的方法就是这样做

if Level == 5 or Name in ['James','Chris','Alex'] or (Name in ['John','Mike'] and Form):
    pass
else:
    # your code here

把所有东西都用圆括号括起来,一开始就用not怎么样。这样,就不需要反转任何运算符。你知道吗

if not (Level == 5 or Name in ['James','Chris','Alex'] or (Name in ['John','Mike'] and Form)):

相关问题 更多 >