XOR of three values

前端 未结 8 936
执念已碎
执念已碎 2020-12-02 23:04

What is the simplest way to do a three-way exclusive OR?

In other words, I have three values, and I want a statement that evaluates to true IFF only one of

8条回答
  •  情歌与酒
    2020-12-02 23:21

    Better yet on Python:

    result = (1 if a else 0)+(1 if b else 0)+(1 if c else 0) == 1
    

    This can be used also on if statements!

    It saved my day for CLI mutually exclusive arguments through Click (everyone hates click)

提交回复
热议问题