- Collapse Code
import random
b1 = (random.randint(1,1000) % 2 == 0)
b2 = (random.randint(1,1000) % 2 == 0)
# Notice the variable names: _a, _b, _c, etc.
# NOT a, b, c, etc.
_a = random.randint(-100,100)
_b = random.randint(-100,100)
_c = random.randint(-100,100)
_d = random.randint(-100,100)
_e = random.randint(-100,100)
print("b1 is " + str(b1))
print("b2 is " + str(b2))
print("_a is " + str(_a))
print("_b is " + str(_b))
print("_c is " + str(_c))
print("_d is " + str(_d))
print("_e is " + str(_e))
print()
"""
PART 1: BOOLEAN EXPRESSION PRACTICE
Place your line of code UNDERNEATH the
instruction comment. Follow the format I
provide for you here, with the expression
as part of the String literal and in parentheses
after the plus sign for concatenation
"""
# EXAMPLE
# Write an expression that is true if
# b1 and b2 are BOTH true
print("b1 and b2 is " + str(b1 and b2))
# Write an expression that is true if
# EITHER b1 OR b2 (but NOT both!) is true
# Write an expression that is true if
# EITHER b1 or b2 (or both) is false
# Write an expression that is true if _e
# is LESS THAN OR EQUAL to _c
# Write an expression that is true if _a
# and _d are EQUAL
# Write an expression that is true if the
# sum of _a and _c is GREATER THAN _c
# Write an expression that is true if *neither* of
# the following conditions apply:
# _a and _d are EQUAL
# the sum of _a and _c is GREATER THAN _c
print()
"""
PART 2: BASIC IF/ELSE STATEMENTS
NOTE: An "else" is only necessary if an
alternative is explicitly called for.
"""
# EXAMPLE
# if b1 is true, print out: b1 is true!
if b1:
print("b1 is true!")
# if b2 is false, print out: b2 is false!
# if b1 and b2 are both false, print out:
# b1 and b2 are BOTH false!
# if at least one of b1 and b2 is false, print out:
# At least one, b1 or b2, is false!
# Otherwise, print out: b1 and b2 are BOTH true!
print()
"""
PART 3: NESTED IF/ELSE STATEMENTS
See example code below for an illustration
of how to translate my comments into actual
conditional code. It will give you clues
as to how different parts should be nested.
"""
# if _a is less than _b, print out:
# _a is less than _b
# if _a is less than _c as well, also print out:
# _a is less than _c, too!
# Otherwise, print out:
# _a may be less than _b, but it is greater than or equal to _c
#
# However, if _a is NOT less than _b, print out:
# _a is greater than or equal to _b, but we don't know its relation to c
# In addition, if _a is less than _d, print out:
# _a is less than _d
# YOUR CODE GOES HERE
"""
EXAMPLE CODE
if _d is greater than or equal to _e, print out:
_d is greater than or equal to _e
if _d is less than or equal to _c, also print out:
_d is also less than or equal to _c
However, if _d is less than _e, print out:
_d is less than _e
In addition, if _d is greater than _b, also print out:
...but _d is greater than _b
Otherwise, also print out:
...and _d is less than or equal to _b
if _d >= _e:
print ("_d is greater than or equal to _e")
if _d <= _c:
print ("_d is also less than or equal to _c")
else:
print ("_d is less than _e")
if _d > _b:
print ("...but _d is greater than _b")
else:
print ("...and _d is less than or equal to _b")
"""
- Collapse Code
+ Expand Code
import random
b1 = (random.randint(1,1000) % 2 == 0)
b2 = (random.randint(1,1000) % 2 == 0)
# Notice the variable names: _a, _b, _c, etc.
# NOT a, b, c, etc.
_a = random.randint(-100,100)
_b = random.randint(-100,100)
_c = random.randint(-100,100)
.........
Your task, then, is to follow the directions in the comments in order to make changes to the code that will result in the desired output being printed to the screen.
- Write the print statements that print the expression itself and whether it is True or False.
- Write the simple if and if/else statements
- Write the nested if/else statements
Some of the instructions in the comments are frequently misinterpreted, so make certain that you are present in class to receive the detailed explanations that I intend to give in class!
Note that because b1 and b2 are being assigned True or False at random, while the int variables are also being assigned random values, the output will look different each time you run the program. To this end, I have provided multiple examples of how the output could look.
Note also that some parts of the sample output are blacked out like this: __________ This is because you are being asked to write the boolean expression twice -- once as part of the output string text and once to actually be evaluated as True or False. The blacked out portions are just to avoid giving away the answer in the sample output.
Program output examples:
- Collapse Output
b1 is False
b2 is True
_a is 90
_b is -22
_c is -39
_d is 45
_e is -27
b1 and b2 is False
__________ is True
__________ is True
__________ is False
__________ is False
__________ is True
__________ is False
At least one, b1 or b2, is false!
_a is greater than or equal to _b, but we don't know its relation to c
- Collapse Output
b1 is False
b2 is False
_a is 93
_b is 82
_c is 32
_d is -18
_e is -49
b1 and b2 is False
__________ is False
__________ is False
__________ is True
__________ is False
__________ is True
__________ is False
b2 is false!
b1 and b2 are BOTH false!
At least one, b1 or b2, is false!
_a is greater than or equal to _b, but we don't know its relation to c
- Collapse Output
b1 is False
b2 is True
_a is 18
_b is 16
_c is 81
_d is -80
_e is 48
b1 and b2 is False
__________ is True
__________ is True
__________ is True
__________ is False
__________ is True
__________ is False
At least one, b1 or b2, is false!
_a is greater than or equal to _b, but we don't know its relation to c
- Collapse Output
b1 is True
b2 is False
_a is -51
_b is -5
_c is -41
_d is -73
_e is -50
b1 and b2 is False
__________ is True
__________ is True
__________ is True
__________ is False
__________ is False
__________ is True
b1 is true!
b2 is false!
At least one, b1 or b2, is false!
_a is less than _b
_a is less than _c, too!
- Collapse Output
b1 is True
b2 is False
_a is -22
_b is -84
_c is -51
_d is -49
_e is -81
b1 and b2 is False
__________ is True
__________ is True
__________ is True
__________ is False
__________ is False
__________ is True
b1 is true!
b2 is false!
At least one, b1 or b2, is false!
_a is greater than or equal to _b, but we don't know its relation to c
- Collapse Output
b1 is False
b2 is True
_a is -48
_b is -97
_c is 72
_d is 92
_e is -5
b1 and b2 is False
__________ is True
__________ is True
__________ is True
__________ is False
__________ is False
__________ is True
At least one, b1 or b2, is false!
_a is greater than or equal to _b, but we don't know its relation to c
_a is less than _d
In at least 150 words, please address the following questions in memo.txt: - How did the process of creating these programs go for you?
- What were your main challenges and how did you overcome them?
- What did you learn that may be of use as you move along in this class?