15

I want to do multiple comparisons for a logical condition in python but I am not sure of the right way round for the and and or. I have 2 statements.

Statement 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: 

Statement 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

Is or-ing the two ands the correct way to go about it?

Thanks.

mark mcmurray
  • 1,581
  • 4
  • 18
  • 28
  • 5
    `PAB > BAC and PAB< PAC` is better written as `BAC < PAB < PAC` – mgilson May 07 '13 at 16:54
  • `PAB`, `PAC` and `BAC` are angles of 3 triangles with common vertices. `PAB > BAC and PAB< PAC` can definitely be true. – mark mcmurray May 07 '13 at 16:56
  • @markmcmurray: I misread that as `a < b and b < a` - your variable names are too confusing! – Eric May 07 '13 at 17:12
  • @Eric The variable names aren't confusing. A, B and C are points on a triangle, or something along those lines. – David Heffernan May 07 '13 at 17:16
  • @Eric They're not confusing in the context of how I am using them with triangles where the vertices are single letters (eg A,B,C,P) – mark mcmurray May 07 '13 at 17:29
  • By confusing, I meant "easy to misread". You're correct, those are completely appropriate names. – Eric May 07 '13 at 18:06
  • Sorry, I probably sounded a little rude in that previous comment, I was just stating that even though they mightn't be the clearest here, this is the clearest way to do them in the context of my code. – mark mcmurray May 07 '13 at 18:11

1 Answers1

29

Looking at statement 1, I'm assuming you mean:

if (PAB > BAC and PAB< PAC) or (PAB > BAC and PAC>BAC): 

In which case, I'd probably write it like this (using chained comparisons, docs: python2, python3):

if (BAC < PAB < PAC) or min(PAB,PAC)>BAC:

You can use an analogous form for statement 2.

Having said that, I cannot make your comments in the question's code match up with my interpretation of your conditionals, so it's plausible I don't understand your requirement.

maxkoryukov
  • 4,205
  • 5
  • 33
  • 54
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490