Reputation: 35
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.
Upvotes: 1
Views: 3777
Reputation: 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
Upvotes: -1
Reputation: 1157
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.
Upvotes: 2
Reputation: 155333
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).
Upvotes: 2
Reputation: 1696
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.
Upvotes: 0
Reputation: 60143
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:
Upvotes: 1