I have actually a python script that can receive one of two people zero or 3 command heat arguments. (Either it runs on default behavior or demands all three values specified.)

What"s the appropriate syntax for something like:

if a and also (not b or no c) or b and (not a or not c) or c and also (not b or no a):?




You are watching: B is not between a and c

*

*

If you average a minimal form, go through this:

if (not a or not b or not c) and also (a or b or c):Which translates the location of your question.

UPDATE: as correctly said by Volatility and also Supr, friend can use De Morgan"s law and obtain equivalent:

if (a or b or c) and not (a and b and also c):My advice is to usage whichever kind is more significant to you and also to various other programmers. The first means "there is something false, but additionally something true", the second "There is something true, however not everything". If ns were to optimize or do this in hardware, ns would select the second, right here just pick the many readable (also soaking up consideration the conditions you will be testing and also their names). Ns picked the first.


re-superstructure
improve this answer
follow
edited might 14 "13 at 23:04
answered may 13 "13 at 12:35
*

Stefano SanfilippoStefano Sanfilippo
29.8k77 yellow badges7272 silver- badges7979 bronze title
15
| display 10 an ext comments
241
How about:

conditions = if any(conditions) and also not all(conditions): ...Other variant:

if 1
share
enhance this prize
follow
answered might 13 "13 in ~ 12:32

*

defuzdefuz
24.8k88 yellow badges3636 silver badges5959 bronze badges
10
| show 5 much more comments
116
This question already had numerous highly upvoted answers and an welcomed answer, but every one of them so far were distracted by various ways to to express the boolean problem and also missed a crucial point:

I have a python manuscript that can receive either zero or three commandline arguments. (Either it operation on default behavior or demands all threevalues specified)

This logic need to not be the responsibility of your code in the an initial place, fairly it need to be tackled by argparse module. Don"t bother composing a facility if statement, rather prefer come setup your dispute parser something favor this:

#!/usr/bin/env pythonimport argparseparser = argparse.ArgumentParser()parser.add_argument("--foo", nargs=3, default=<"x", "y", "z">)args = parser.parse_args()print(args.foo)And yes, it must be one option not a positional argument, because it is after all optional.

edited: To resolve the worry of LarsH in the comments, below is an instance of how you might write the if friend were details you wanted the interface with one of two people 3 or 0 positional args. I am that the opinion the the previous user interface is better style, because optional debates should it is in options, but here"s an different approach because that the sake of completeness. Keep in mind the overriding kwarg intake when producing your parser, since argparse will certainly auto-generate a misleading consumption message otherwise!

#!/usr/bin/env pythonimport argparseparser = argparse.ArgumentParser(usage="%(prog)s <-h> ")parser.add_argument("abc", nargs="*", help="specify 3 or 0 items", default=<"x", "y", "z">)args = parser.parse_args()if len(args.abc) != 3: parser.error("expected 3 arguments")print(args.abc)Here room some consumption examples:

# default casewim
wim-zenbook:/tmp$ ./three_or_none.py <"x", "y", "z"># explicit casewim
wim-zenbook:/tmp$ ./three_or_none.py 1 2 3<"1", "2", "3"># example failure modewim


See more:
Shapes With Parallel And Perpendicular Lines, Parallel And Perpendicular Lines

wim-zenbook:/tmp$ ./three_or_none.py 1 2 usage: three_or_none.py <-h> three_or_none.py: error: meant 3 arguments