So I'm trying to implement the logical operator XOR in Python. I first ask the user for how many inputs they want to test (4 - TT, TF, FT, FF). I know that XOR evaluates T&T->F, T&F->T, F&T->T, F&F->F.
I store the first boolean that I get as input from the user in a variable P as a string. I then convert to boolean. As so...
P = input("Enter boolean number 1: ")
P = bool(P)
I don't cast the input as bool right away since any non-empty string will result in True regardless of the string being "False."
I have the XOR truth table established for the four possible conditions mentioned above as four separate if statements and then just print("P xor Q is False" or "P xor Q is True") all depending on the truth table for XOR.
All this logic is inside a for loop that counts down by 1 until 0 from the user's input of how many inputs they wanted to make.
When I run this program, regardless of what the user enters, the print statement is "P xor Q is False."
I just can't figure out why! I feel the solution is pretty simple which is what is bothering me, so any help in the right direction would be appreciated, thank you!
Here's my code:
numOfInputs = int(input("Enter the number of inputs: "))
for num in range(numOfInputs, 0, -1):
print()
P = input("Enter the first boolean: ")
P = bool(P)
Q = input("Enter the second boolean: ")
Q = bool(Q)
print()
if(P == True and Q == True):
print("P xor Q is False")
if(P == True and Q == False):
print("P xor Q is True")
if(P == False and Q == True):
print("P xor Q is True")
if(P == False and Q == False):
print("P xor Q is False")