CS 110 Lab Project 4
Boolean Logic and Conditionals
In this lab, you will practice with Boolean logic, which is the logic of a True/False value system. The first set of problems will not be about Python, but will just involve working with Boolean expressions and truth tables enumerating all the possible values of the inputs and the resultant values of the expressions.
The next set of problems will be basic Python programs, much like the input-compute-output problems of Projects 2 and 3, where you are given a word problem that requires the use of a conditional to solve the problem, and you are to write the corresponding Python program.
We finish out this homework set with the (promised) Rock/Paper/Scissors program, which will allow us to combine our graphics programming skills with the new Image objects and with the use of conditionals in the resulting program.
For each of the programs, I expect good variable names and good commenting practices. There should be comments for the program itself, and we should comment each function so that the comments describe what the function does in terms of its parameters (sometimes called the function's 'givens') and details any return value and any output performed by the function.
Each program should be structured, at a minimum, as a definition of a main function followed by its invocation. I encourage you to use functions to help package together related/coherent sets of Python statements, and will be looking for good use of functions in my grading.
Problem 1: Boolean Expression Simplification
In this writeup, we will use the 'v' symbol for OR and the '^' symbol for AND and the ¬ symbol for NOT. The NOT modifies only the term that immediately follows the symbol, and so, if I want the negation of something more complicated than a simple boolean variable, I must use parenthesis to group a larger expression together.
Precedence rules for boolean expressions are (1) negation is always evaluated first, (2) AND is evaluated next, and (3) OR is evaluated last. Parenthesis can change this order.
Simplify the following boolean expressions (labeled as Q) as much as possible. That is, your task is to find the smallest expression that is equivalent to the stated expression. You may want to write out a truth table to check your solutions. The first two solutions are given, along with explanations, but you are not required to provide explanations for your answers. For the first three problems in this homework set, submit your solutions as a text file called logic.rtf.
1. Q = A v False
Solution: Q = A (if A is true, Q is true, and if A is false, Q is false, and thus A and Q are the same)
2. Q = A ^ False
Solution:
Q = false (regardless of whether A is true or false, Q is false)
3. Q = A v true
Solution: Q =
4. Q = A ^ true
Solution: Q =
5. Q = A^ ¬A
Solution: Q =
6. Q = A v ¬A
Solution: Q =
7. Q = ¬(¬A)
Solution: Q =
8. Q = (A v B) ^ (A v ¬B)
Solution: Q =
9. Q = ¬(¬A v ¬B)
Solution: Q =
10. Q = ¬(¬A ^ ¬B)
Solution: Q =
Problem 2: Boolean Expression Equivalence
Indicate which of the following equations are correct for all truth values of A, B and C, and which are not. Justify your answer. To be clear, a justification that two expressions are different simply requires you to specify truth values for the variables such that the two expressions disagree. A justification that they are the same requires you to create a complete truth table in which all entries for the two expressions match.
1. (A v B) ^ ¬(A ^ B) = (A ^ ¬B) v (¬A ^ B)
2. (A v B) ^ C = A ^ (B v C)
Problem 3: Find the Boolean Expression
For both problems, generate a boolean expression that matches the specified output. Your solutions should be added to logic.rtf.
1.
A |
B |
? |
T |
T |
T |
T |
F |
T |
F |
T |
F |
F |
F |
T |
2.
A |
B |
C |
? |
T |
T |
T |
F |
T |
T |
F |
F |
T |
F |
T |
T |
T |
F |
F |
T |
F |
T |
T |
F |
F |
T |
F |
F |
F |
F |
T |
T |
F |
F |
F |
F |
Problem 4: Conditionals in Python Programs
1. (GradeReport.py) Write a full Python program (main() function definition with main() invocation) with the following functionality; prompt the user for their test grade. If their grade is 90 or above, print "You got an A!" If the grade is between 80 and 89, print "You got a B!" If the grade is between 70 and 79, print "You got a C." And if the grade is below 70, print "Uh oh... you should probably see me." One and only one output should be printed for any particular value.
2. (Divisible.py) Write a full Python program that prompts the user to enter an integer. Then report to the user whether that number is divisible by 2, 3 and 5. For example, if the user enters 15, the output should be "15 is not divisible by 2, is divisible by 3, and is divisible by 5." If the user enters 64, the output should be "15 is divisible by 2, is not divisible by 3, and is not divisible by 5." Use only 3 if-statements.
3. (BMI.py)
Write a full Python program that, given a user's weight and height, entered in response to prompts from the program, then calculates and outputs the BMI corresponding to that height and weight, along with a message indicating whether the resultant BMI indicates an optimal weight, an underweight condition, or an overweight condition. BMI is calculated with the formula:
A sedentary person's weight is considered to be optimal if his or her BMI is between 18.5 and 25. If the BMI is less than 18.5, the person is considered to be underweight. If the BMI is greater than 25, the person is considered to be overweight.
Problem 5: Rock / Paper / Scissors
Write the game Rock, Paper, Scissors. In a file called RPS.py, write a program that does the following:
- The user interface for the game really just consists of a set of "buttons" placed within a graphics window, along with an area where the program can prompt the user on what to do, as well as display the result. There will be four such "buttons" in the window: one each for the user's selection of rock, paper, or scissors, and one 'Exit' button.
- In thinking about the facilities of the graphics.py module, we don't have buttons as an object, so what we need to do, as Python programmers, is to draw our "button" using the graphics objects we have and then inform the user to click on one of the buttons. We will use the getMouse() function to determine, from the returned Point, where the user clicked. We then must look at the x and y coordinates of the user-clicked point and use conditionals to see if the point is in the area bounded by the first button, the area bounded by the second button, etc. Once we find which button area was clicked, we can then perform the desired action associated with the button.
- Ultimately, we want to have jpeg images to represent the buttons of the rock, paper, and scissors (found on the internet and sized appropriately). But to start, simply use Rectangles for each of these three buttons, as well as for the Exit button.
- Be sure and use Text objects to label all your buttons. You may design the layout of your graphics window as you please with regard to the placement of the buttons and the placement of the informative text to the user.
- The program should begin by drawing all the graphics elements and, in the informative text area, telling the user to click on one of the 'Rock', 'Paper', or 'Scissors' buttons. The program should then wait for a user click.
- When the user clicks, there should be one of five possibilities:
- The user clicked within the Exit button. The program should oblige and close the graphics window and complete its execution.
- The user clicked outside of any of the four buttons. The program should update the informative text area to say the click was not a valid one, and for the user to click again to exit.
- The user clicked one of the three Rock/Paper/Scissors buttons. The program should use random number generation to pick its own Rock/Paper/Scissors (using integers to represent the three choices) choice, and should update the informative text area to let the user know the computer's choice, and to compare the user's choice with the computer's choice and to determine the winner. An indication of the winner should be displayed, but without losing the indication of the computer's choice.
- The program, after the game is over, should tell the user to click the Exit button to exit, and wait until this final mouse click before finishing the program.
- Once you get the program debugged with the simple Rectangle based buttons, enhance it to use the jpeg images of Rock, Paper, and Scissors for the final version.