17

I need to check if more than one of a, b, c, and d are being defined:

def myfunction(input, a=False, b=False, c=False, d=False):
    if <more than one True> in a, b, c, d:
    print("Please specify only one of 'a', 'b', 'c', 'd'.)

I am currently nesting if statements, but that looks horrid. Could you suggest anything better?

5
  • 2
    Perhaps a different function signature is in order. If the user is allowed to make exactly one choice, how about def myfunction(input, choice), where choice is one of "a", "b", "c", "d"?
    – Kevin
    Nov 17, 2014 at 15:29
  • def multiple_true(*args):it = iter(args);return any(it) and any(it) Usage: if multiple_true(a, b, c, d): ... Nov 17, 2014 at 15:36
  • @Martijn Pieters - where is the duplicate?
    – TheChymera
    Nov 18, 2014 at 5:32
  • @TheChymera you are testing the inverse to return an error message; the other tests if exactly one is true.
    – Martijn Pieters
    Nov 18, 2014 at 8:02
  • Yeah, I don't think the inverse counts as a duplicate. Also, it's not even just the inverse, the title implies he is testing for one or two. As you can plainly see, the answers below do not closely resemble the answers he got.
    – TheChymera
    Nov 19, 2014 at 22:25

3 Answers 3

21

Try adding the values:

if sum([a,b,c,d]) > 1:
    print("Please specify at most one of 'a', 'b', 'c', 'd'.")

This works because boolean values inherit from int, but it could be subject to abuse if someone passed integers. If that is a risk cast them all to booleans:

if sum(map(bool, [a,b,c,d])) > 1:
    print("Please specify at most one of 'a', 'b', 'c', 'd'.")

Or if you want exactly one flag to be True:

if sum(map(bool, [a,b,c,d])) != 1:
    print("Please specify exactly one of 'a', 'b', 'c', 'd'.")
4
  • Since int(True) is always 1, this will work, why the downvote? +1.
    – Maroun
    Nov 17, 2014 at 15:28
  • 1
    Check your parenthesis. Nov 17, 2014 at 15:29
  • 1
    I didn't downvote, but sum([a,b,c,d]) != 1 is also true if all values are false. This is slightly different logic from the OP's desired "check if more than one" behavior.
    – Kevin
    Nov 17, 2014 at 15:31
  • @Kevin, true but easily fixed.
    – Duncan
    Nov 17, 2014 at 15:32
19

First thing that comes to mind:

if [a, b, c, d].count(True) > 1:
1
  • 1
    I think at PyCon 2014 a speaker said this is the right way to check this and explicitly said sum was the wrong way. I recall it was someone well respected. I wish I remembered specifics
    – Charles L.
    Apr 17, 2020 at 17:05
3

If you want exactly 1 True value:

 def myfunction(input, a=False, b=False, c=False, d=False):
    if filter(None,[a, b, c, d]) != [True]:
        print("Please specify only one of 'a', 'b', 'c', 'd'.)")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.