Two short-answer questions and the python source code in text or screenshots of all programs.
1. (20 points, each 2 points) The following truth table shows various combinations of the values true and false connected by a logical operator (and, or, not). Fill out the table to indicate whether the result of such a combination is True or False:
|
Logical Expression |
Result (True or False)? |
|
True and False |
False |
|
True and True |
|
|
False and True |
|
|
False and False |
|
|
True or False |
|
|
True or True |
|
|
False or True |
|
|
False or False |
|
|
not True |
|
|
not False |
|
2. (20 points, each 2 points) Assume the variables a = 2, b = 4, c = 6, x=0, y=2, and z=4. Fill out the table to indicate whether the result of the following conditions is True or False:
|
Expression (Condition) |
Result (True or False)? |
|
a ==4 or b>2 |
Example answer: True |
|
6<2 and a>3 |
|
|
1!= b and c != 3 |
|
|
a >= -1 and a <= b |
|
|
not (a>2) |
|
|
x or y |
|
|
x and y |
|
|
x or z |
|
|
(x and y) or (y and z) |
|
|
|
3. (25 points) What do these code fragments print? Write the output into the Result column. Explain your result if necessary.
1) (5 points)
n = 1
m = -1
if n<-m:
print(n)
else:
print(m)
|
|
Result:
|
2) (5 points)
n = 1
m = -1
if -n>= m:
print(n)
else:
print(m)
|
|
Result:
|
3) (5 points)
x=0.0
y=1.0
if abs(x-y) < 1:
print(x)
else:
print(y)
|
|
Result:
|
4) (5 points)
import math
x = math.sqrt(2.0)
y = 2.0
if x * x == y:
print('x=', x)
else:
print('y=', y)
|
|
Result: (hint: it may not be the result you think. Run it in pycharm and test your result)
|
5) (5 points)
|
grade = 80 if grade >= 90: print('A') else: print('Not A') elif grade >= 80: print('B') elif grade >= 70: print('C') elif grade >= 60: print('D') |
|
Result:
|
4. (20 points) Use ”if-elif-else” to write a program that prompts the user to input a numeric value and displays the effect of an earthquake, as measures by the Richter scale
|
Richter Scale |
|
|
Value |
Effect |
|
8 or greater |
Most structures fall |
|
7 to 7.99 |
Many buildings destroyed |
|
6 to 6.99 |
Many buildings considerably damaged, some collapse |
|
4.5 to 5.99 |
Damage to poorly constructed buildings |
|
Less than 4.5 |
No destruction of buildings |
· (5 points) Draw a flowchart to understand the program selection flow
· (10 points) Write your program here, or copy/paste a screenshot of your Program:
· (2 points) Screenshot of a Program Run:
· (3 points) Save the program as “earthquake.py”. Upload the .py file as part of your submission.
5. (20 points) Write a program that reads an input temperature value, then reads an input letter (C for Celsius or F for Fahrenheit). Print whether the state of matter is liquid, solid, or gaseous at the given temperature at sea level. Let:
C = (F -32)*5/9
Solid: C<=0
Gaseous: C>=100
Liquid: 0<C<100
· (5 points) Draw a flowchart to understand the program selection flow
· (10 points) Write the program here, or copy/paste the screenshot of your program
· (2 points) Screenshot of a Program Run:
· (3 points) Save the program as “state.py”. Upload the .py file as part of your submission.
2