BigMan_13
BigMan_13

Reputation: 5

Compose a function odd ( ) that takes three bool arguments and returns True if an odd number of arguments are True and False otherwise

I am confused to what exactly I am being asked so I may be out of the ball park with what I have. Any help is greatly appreciated

def odd(1,2,3):
if 1 or 3:
    return True
if 0 or 2:
    return False

Upvotes: 0

Views: 2125

Answers (1)

Nullman
Nullman

Reputation: 4279

according to the question, your function receives 3 inputs of type bool, that is they can be True or False. so what you will want to do is xor them together like this:

def odd(par1, par2, par3):
    return par1 ^ par2 ^ par3

why xor? you should read up a bit on boolean algebra. in short though, xoring 3 bools will return true only if the number of True values is odd

the way you use it is:

if odd(x,y,z):
    print 'odd'
else:
    print 'even'

Upvotes: 3

Related Questions