if语句在单行上进行多次逻辑比较

mar*_*ray 8 python

我想在Python中的逻辑条件做多重比较,但我不知道正确的方式圆了的andor.我有2个陈述.

声明1:

#if PAB is more than BAC and either PAB is less than PAC or PAC is more than BAC
if PAB > BAC and PAB< PAC or PAB > BAC and PAC>BAC: 
Run Code Online (Sandbox Code Playgroud)

声明2:

#if PAB is more than BAC and PAC is less than PAB or if PAB is less than BAC and PAC is less than BAC
if PAB >BAC and  PAC<PAB or PAB<BAC and  PAC<BAC
Run Code Online (Sandbox Code Playgroud)

是不是正确的两种方式去做呢?

谢谢.

Dav*_*nan 16

看一下声明1,我假设你的意思是:

if (PAB > BAC and PAB< PAC) or (PAB > BAC and PAC>BAC): 
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我可能会这样写(使用链式比较,docs:python2,python3):

if (BAC < PAB < PAC) or min(PAB,PAC)>BAC:
Run Code Online (Sandbox Code Playgroud)

您可以在声明2中使用类似的表单.

话虽如此,我不能在问题的代码中使你的评论与我对你的条件的解释相符,所以这似乎是合理的我不明白你的要求.