Program of the Day #5

In the lecture video, you saw truth tables for the and and or operators. Another boolean operator that doesn’t have a special keyword in Python is called “exclusive or”, or “XOR” for short. This operator evaluates to True if exactly one of its operands is True, and evaluates to False if its operands are both True or both False. Fill in the truth table for the XOR operator:

a xor b b is True b is False
a is True
a is False

Now, write a program that takes two arguments, which you should assume are either T or F, representing the boolean values True and False. Your program should compute the xor operator on the inputs and print True or False accordingly. Example run:

>>> %Run P05_xor.py F T
True
>>>

Note: The intended solution to this program does not use if statements - which we’ll cover next lecture. A solution using ifs can pass the tests and receive credit, I encourage you to solve it using only boolean operators instead, even if you know how to use conditionals.

Other Practice Problems

  1. Suppose your program contains variables a and b that have boolean values. Which of the following evaluate to a XOR b, according to the definition of XOR in the Program of the Day prompt above?

    1. not (a and b) and not(not a and not b)
    2. a or b
    3. a and b or not (a or b)
    4. (a or b) and not (a and b)
    5. a or b and not a and b
  2. Way back in Lecture 1, recall that we started writing a math quiz program:

    Write a math quiz program that works as follows: it begins by printing an arithmetic problem of your choosing. Then, it prompts the user to enter an answer. Finally, once they have pressed enter, the program prints a message showing the correct answer.

    With boolean comparison operators, we’re one step closer to being able to check the user’s answer. Write the math quiz program so that it prints True if the user’s answer is correct and False if the user’s answer is not correct. A couple sample runs of the program are given below:

    What is 4 * 6? 24
    Your answer is True!
    What is 4 * 6? 2
    Your answer is False!