Question

In: Computer Science

Write a program in Python to practice evaluating some logical expressions and determine whether the expressions...

Write a program in Python to practice evaluating some logical expressions and determine whether the expressions are equivalent.

  1. Read in a 0 or 1 integer value for each of the following variables: A, B, C, and D.

  1. Use an if…else statement to change the 0 and 1 integer values into True and False Boolean values. Booleans in Python have to have this capitalization (True/False, not true/false or TRUE/FALSE) and shouldn’t contain double quotes (which makes them a String instead of a Boolean).

  1. Use the converted True/False values to compute:

(( A ¬C) D)

in your code.

  1. Use the values to compute:

(( ¬A) B)

in your code.   HINT: There no Boolean in Python. How would you write the code for the operator using if statements and the 3 standard Boolean operators: AND, OR, and NOT?

  1. Check to make sure that your program successfully calculates the result of both expressions.

  1. If the truth values for the entered combination of A, B, C, and D values is the same for both expressions, (( A ¬C) D ) and (( ¬A) B), print a message saying: “matches”

Otherwise print a message saying “untrue.” At this stage, your program will produce just one line of output per run.

  1. We know that the operator produces an answer of true ONLY when the two expressions produce the same result for EVERY combination of input values.

Change your program so instead of asking the user to enter an A, B, C, and D value, the computer uses loops to iterate through all the possible combinations of A, B, C, and D values. HINT: What possible values can A take on? What possible values can B take on? Nested loops will be your friend here.

For each set of A, B, C, and D values, print these input values on one line along with the computed match/untrue message for that line.   

Example: (( A ¬C) D)

Input                                 express 1           express 2           result

0      0      0      0              0                      1                      untrue

0      0      0      1              1                      1                      matches

…..

  1. After all of the possible combinations of input have been considered, print a message saying “equivalent” if you find that the two expressions are equivalent. Print a message saying “not equivalent” if you find that the two expressions are not equivalent.

Solutions

Expert Solution

Here is the Python code:

A = int (input ("Input A, either 0 or 1:"))
B = int (input ("Input B, either 0 or 1:"))
C = int (input ("Input C, either 0 or 1:"))
D = int (input ("Input D, either 0 or 1:"))

A = False if A == 0 else True
B = False if B == 0 else True
C = False if C == 0 else True
D = False if D == 0 else True

def firstexpr (A, C, D):
    return ((A or (not C)) and D)
    
Expr1 = firstexpr (A, C, D)

def xor (firstbool, secondbool):
    return (firstbool != secondbool)

Expr2 = xor (not A, B)

if (Expr1 == Expr2):
    print ("Matches")
else:
    print ("Untrue")

Equivalent = True

for a in False, True:
    for b in False, True:
        for c in False, True:
            for d in False, True:
                lhs = int (firstexpr (a, c, d))
                rhs = int (xor (a, b))
                if lhs != rhs:
                    Equivalent = False
                comparison = "Matches" if lhs == rhs else "Untrue"
                print (int (a), int (b), int (c), int (d), "\t", lhs, rhs, comparison )
                
if Equivalent:
    print ("Equivalent")
else:
    print ("Not equivalent")
    

Related Solutions

I need to write an interpreter for arithmetic expressions in Python. This program receives an input...
I need to write an interpreter for arithmetic expressions in Python. This program receives an input string with an arithmetic expression and does: 1. prints a ">>>" as if it was a terminal 2. lets us allocate a variable to a a numeric value (as "a = 3") then it stores {'a':3} to a dictionary memory 3. whenever it receives an expression or input, transform it to postfix notation and print it (even when allocating variables) 4. it is able...
For Python: In this assignment you are asked to write a Python program to determine the...
For Python: In this assignment you are asked to write a Python program to determine the Academic Standing of a studentbased on their CGPA. The program should do the following: Prompt the user to enter his name. Prompt the user to enter his major. Prompt the user to enter grades for 3 subjects (A, B, C, D, F). Calculate the CGPA of the student. To calculate CGPA use the formula: CGPA = (quality points * credit hours) / credit hours...
in PYTHON Determine whether a number provided by the user is perfect or not. write a...
in PYTHON Determine whether a number provided by the user is perfect or not. write a program that: 1. Ask the user for a positive integer 2. Compute the list of integer divisors of the given number 3. Check whether the given number is perfect 4. Communicate the result to the user (something like ‘Your number…. Is a perfect number’ or ‘Your number ….. is not a perfect number’. 5. In any case, communicate to the user the list of...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The...
Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The program for this project should consist of three classes. -The main class should create a GUI that allows the user to input an expression in either prefix or postfix and convert it to postfix or prefix, respectively. -The other class should contain the code to perform the conversions. --->The pseudocode for prefix to postfix, which requires two stacks, is shown below: tokenize the string...
1. There are two steps for evaluating argument, the first step is to determine the logical...
1. There are two steps for evaluating argument, the first step is to determine the logical strength of the argument and the second step is to determine the truth value of the premises. Please discuss whether this 2-step evaluation is good. 2. Informal fallacy can be divided into 4 classes: inconsistence, irrelevancy, insufficiency and inappropriate presupposition. It is called "4I" classification. Discuss whether it Is a good classification.
PYTHON Write a program that accepts a range of input from the user and checks whether...
PYTHON Write a program that accepts a range of input from the user and checks whether the input data is sorted or not. If the data series is already sorted your program should print “True” or should print “False” otherwise. You should not use any sort function for this program. Input: How many numbers you want to input: 3 # user input 3 Input the number: 5 Input the number: 2 Input the number: 7 Output: False
Write a method for binary tree in Python that can determine whether a binary tree is...
Write a method for binary tree in Python that can determine whether a binary tree is a binary search tree or not. The input should be a binary tree. The output should be true or false. True= binary tree meets the criteria to be a binary search tree. False= does not meet the criteria to be a binary search tree.
Write a Python program called arecongruent.py that determines whether two integers a and b are congruent...
Write a Python program called arecongruent.py that determines whether two integers a and b are congruent modulo n. Your code must work as follows: From the command prompt the user runs the program by typing python arecongruent.py and then your program interface prompts the user for the values of a, b, and n. The program outputs either True or False, and the values of a mod n and b mod n. Submit your Python source code arecongruent.py. NOTE: please include...
Lab 7. Boolean Expressions a) Write a program that evaluates the following expressions. Assign reasonable values...
Lab 7. Boolean Expressions a) Write a program that evaluates the following expressions. Assign reasonable values to the variables. Print the results. a<b≥c , √a−7 b2 ≠c , d∨e∧f , a<b∨¬d ∧means and, ∨means inclusive or, ¬ means not. b) Write a program that asks a user whether he or she wants to become a Java programmer and determines if the user typed “yes” (Print true if yes and false otherwise.) Don't use the if statement here
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT