1

Given two input boolean values I want to print out the following results:

True True -> False
True False -> False
False True -> False
False False -> True

I tried doing this:

if boolInput1 and boolInput2 == True:
    print(False)
elif boolInput1 == True and boolInput2 == False:
    print(False)
elif boolInput1 == False and boolInput2 == True:
    print(False)
elif boolInput1 and boolInput2 == False:
    print(True)

but it doesn't work as this is the output:

Test  Input    Expected Actual 
1   True True   False   False
2   True False  False   False
3   False True  False   False
4   False False True    False

I've tried searching for an answer online but can't find anything.

3
  • print(not boolInput1 and not boolInput2)
    – cs95
    Commented Jul 7, 2017 at 0:38
  • @cᴏʟᴅsᴘᴇᴇᴅ That would return True for the first case and False for all the others.
    – user94559
    Commented Jul 7, 2017 at 0:43
  • @smarx Sorry, I misread.
    – cs95
    Commented Jul 7, 2017 at 0:44

5 Answers 5

2

boolInput1 and boolInput2 == False doesn't do what you think. The == binds more tightly than the and, so you're testing "is boolInput1 (truthy), and is boolInput2 equal to False", when you want "is boolInput1 False and boolInput2 False too?", which would be expressed boolInput1 == False and boolInput2 == False or more Pythonically, not boolInput1 and not boolInput2.

Really, you're making this harder than it needs to be. All of your code could simplify to just:

print(not boolInput1 and not boolInput2)

or extracting the not if you prefer it:

print(not (boolInput1 or boolInput2))

No if, elif, else or any other blocks required.

Generally speaking, explicitly comparing to True or False is not Pythonic; just use implicit "truthiness" testing to work with any types. Since you need not here anyway, the end result will always be True or False, even if the inputs aren't booleans at all, where directly comparing to True or False will make inputs like 2, None, or [] behave differently from the way they traditionally behave in "truthiness testing" (they'd be truthy, falsy and falsy respectively).

2

This could be much simpler.

if bool1 or bool2:
    print(False)
else:
    print(True)

You can also, I believe, do

print(not(bool1 or bool2))

which is simpler still.

7
  • I think you have that backwards.
    – user94559
    Commented Jul 7, 2017 at 0:42
  • I'm not sure if the edit has been made yet, but this is still wrong. I believe all the other answers have it right. :-)
    – user94559
    Commented Jul 7, 2017 at 0:52
  • As a test case, what if bool1 is True and bool2 is False? The correct answer is False, but your code would print True.
    – user94559
    Commented Jul 7, 2017 at 0:54
  • @smarx, geesh, I'm just failing here, aren't I? Let me fix this.
    – auden
    Commented Jul 7, 2017 at 0:55
  • No need to fix... there are already several correct answers. But if you want to be different, go for if bool1 or bool2: print(False) and print(not(bool1 or bool2)).
    – user94559
    Commented Jul 7, 2017 at 0:56
1

How about this?

print(not boolInput1 and not boolInput2)

The issue with your code is here:

elif boolInput1 and boolInput2 == False:
    print(True)

That would work if it read:

elif boolInput1 == False and boolInput2 == False:
    print(True)

This line works fine despite having the same kind of issue, because the if boolInput1 does roughly what you want (checks for a truthy value).

if boolInput1 and boolInput2 == True:

It might be better to write it this way to be more consistent with your other checks:

if boolInput1 == True and boolInput2 == True:
0
0

elif boolInput1 and boolInput2 == False: isn't doing what you think it's doing.

Each side of the and are evaluated as separate booleans.

To condense what the computer is doing on that statement:

boolInput1 and boolInput2 == False
False and False == False
False and True
False #Does not enter if Statement

This should show you that your logic on all 4 is actually wrong and there are ways to mess it up. Try to avoid boolean == true kind of statements wherever possible, and just say if boolean

Working version:

if boolInput1 and boolInput2:
    print(False)
elif boolInput1 and not boolInput2:
    print(False)
elif not boolInput1 and boolInput2:
    print(False)
elif not boolInput1 and not boolInput2:
    print(True)

Though depending on your reason for this code, there are even more simple ways to do it.

-1

Your truth table is just exclusive-or.

bool1 ^ bool2

#!/usr/bin/env python
bools = [True, False]
for bool1 in bools:
    for bool2 in bools:
        print(bool1, bool2, bool1 ^ bool2)

True True False
True False True
False True True
False False False
1
  • This is clearly wrong.
    – nullptr
    Commented May 2, 2023 at 10:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.