-5

XOR logic The exclusive or(XOR) is a logical operator that accepts two inputs of type Boolean. The operator outputs ‘True’ if either of the inputs are true but not both. Write a function that can evaluate and prints the XOR given two Boolean inputs.

def logical_xor(a,b):
    a = input("enrter an algebric boolean: ")
    b = input("enrter another algebric boolean: ")
    if bool(a) == bool(b):
        print(bool(a) or bool(b))
    elif bool(a) != bool(b):
        print()

logical_xor(True,True)
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • def logical_xor(a,b): a =input("enrter an algebric boolean: ") b =input("enrter another algebric boolean: ") if bool(a) == bool(b): print(bool(a) or bool(b)) elif bool(a) != bool(b): print() logical_xor(True,True) – Sammy Ab Dec 10 '21 at 17:43
  • 4
    Don't add code to comments. [edit] and put the code in your question – 001 Dec 10 '21 at 17:44
  • If you pass the arguments to the function, there's no need to ask for them as inputs. – 001 Dec 10 '21 at 17:46
  • 2
    @SammyAb you should actually ask a question, your post doesn't actually ask for help for anything and will probably be closed if you don't edit it to meet this site's guidelines. See [ask] and the [tour]. – Random Davis Dec 10 '21 at 17:47
  • Canonical duplicate: [How do you get the logical xor of two variables in Python?](https://stackoverflow.com/questions/432842/) – Karl Knechtel Oct 17 '22 at 05:50

1 Answers1

1

Since your arguments are already bool then simply comparing to make sure the values are not equal is sufficient

def logical_xor(a, b):
    return a != b

Verifying the truth table

>>> logical_xor(True, True)
False
>>> logical_xor(False, False)
False
>>> logical_xor(True, False)
True
>>> logical_xor(False, True)
True

Some notes about your implementation:

  1. Since a and b are function arguments you should not also input them as it will override the parameters that were passed to your function.
  2. print is not the same thing as return. The latter will return the value which can then be assigned outside the function, the former will simply display the value to the termial/console.
  3. There is no point in converting the arguments to bool since they are already that type given the question definition.
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Please read [answer] and https://meta.stackoverflow.com/questions/334822, and don't attempt to provide an answer to blatantly copied and pasted homework assignments. The fact that the student wrote some code does not entail that there is any **question** here; either the code is decoration, or there is a hidden "please debug the code for me" request. – Karl Knechtel Oct 17 '22 at 05:49
  • Even if we pretend the question was rephrased in the student's own terms and asked properly, it is a common duplicate. – Karl Knechtel Oct 17 '22 at 05:51