Unit 1

Booleans, Conditionals, and Errors

Logical Expressions

[6.1] What is the output of the following code?

        
a = 3 
b = (a != 3) 
print(b)
        
    

[6.2] What will the following expression evaluate to:

        
not(True and True)

True or False

not(False and True)

False and False
        
    

[6.3] Consider the expression not a or b.
Which of the following makes the expression False? (There could be more than one!)

        
A. a=False, b=False
B. a=False, b=True
C. a=True, b=False
D. a=True, b=True
        
    

[6.4] What will the following expressions evaluate to if x = 10? If it evaluates to an error you can write “Error”.

        
(x >= 2) or (“hello” == “HELLO”)

x < 25 and x > 15

not (x > 5 and x <= 10)

15 + "-110" == "15-110"

(x > 7) or ((x**3 > 50) and (x == 20))
        
    

[6.5] For each of the following expressions, fill in the blank ______ with an appropriate Boolean comparator so that the expression evaluates to True

        
3 ______ 4

10 ____ 5

“Jack” ____ “jill”

42 ______ “42”
        
    

Conditional Predicition

[6.6] Given the code below, answer these questions:

        
x = 15 
if x > 10: 
    print("A") 
elif x == 15:
    print("B")
else: 
    print("C")
        

1. What will the code print?
2. Can we print both A, B and C at the same time?
    

[6.7] What is the value of x after the following code runs?

        
x = 5 
if x > 2: 
    x = -3 
if x > 1: 
    x = 1 
else: 
    x = 3
        
    

[6.8] What is the value of x after the following code runs?

        
x = 5 
if x > 0: 
    print("positive") 
elif x == 5: 
    print("equal") 
elif x % 5 == 0: 
    print("divisible by 5") 
else: 
    print("other")
        
    

[6.9] What will the following return given the code definition below:

        
def pos_neg(a, b, negative):
    if negative:
        return a < 0 and b < 0
    else:
        return (a < 0 and b > 0) or (a > 0 and b < 0)
        
    
Function Call Returned Value
pos_neg(1, -1, False)
pos_neg(-1, 1, False)
pos_neg(-4, -5, True)

[6.10] What values of x will allow the code to print “A”? What about to print “B”?

        
if -10 < x and x < 10: 
    print("A") 
elif x <= -10 or x >= 10: 
    print("B")
        
    
Printed Message Condition
"A"
"B"

[6.11] Do the following two snippets of code do exactly the same thing? Assume that temperature already refers to a number. Explain your answer in a few sentences.


#code 1
if temperature > 0: 
    print('warm') 
elif temperature == 0: 
    print('zero') 
else: 
    print('cold')

#code 2
if temperature > 0: 
    print('warm') 

elif temperature == 0: 
    print('zero') 

print('cold')

Conditional Code Writing

[6.12] The Problem: In basketball, three plays score points: a three-point shot, a two-point shot, and a one-point free throw. You just watched a basketball game between the Apples and Bananas, and recorded the number of successful three-point, two-point, and one point plays for each team. If the game was won by the Apples we want to print A, if the game was won by the Bananas we want to print B, and if the game was a tie we want to print T.

        
apple_three = int(input("Enter the # of three points for apple:"))
apple_two = int(input("Enter the # of two points for apple:"))
apple_one = int(input("Enter the # of one points for apple:"))

banana_three = int(input("Enter the # of three points for banana:"))
banana_two = int(input("Enter the # of two points for banana:"))
banana_one = int(input("Enter the # of one points for banana:"))

apple_total = apple_three * 3 + apple_two * 2 + apple_one
banana_total = banana_three * 3 + banana_two * 2 + banana_one

        
    

[6.13] Write the function sleep_in(weekday, vacation): where the parameter weekday is True if it is a weekday, and the parameter vacation is True if we are on vacation. We sleep in if it is not a weekday or we're on vacation. Return True if we sleep in. For example:


sleep_in(False, False) → True
sleep_in(True, False) → False
sleep_in(False, True) → True

[6.14] Write the function monkey_trouble(a_smile, b_smile): We have two monkeys, a and b, and the parameters a_smile and b_smile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return True if we are in trouble.

For example:

            
monkey_trouble(True, True) → True
monkey_trouble(False, False) → True
monkey_trouble(True, False) → False
            
        

[6.15] Write the function sum_double(a, b): Given two int values, return their sum. Unless the two values are the same, then return double their sum.

For example:

            
sum_double(1, 2) → 3
sum_double(3, 2) → 5
sum_double(2, 2) → 8
            
        

[6.16] Write the function makes10(a, b): Given 2 ints, a and b, return True if one if them is 10 or if their sum is 10.

Example:

            
makes10(9, 10) → True
makes10(9, 9) → False
makes10(1, 9) → True
            
        

[6.17] Write the function near_hundred(n): Given an int n, return True if it is within 10 of 100 or 200. Note: abs(num) computes the absolute value of a number.

Example:

            
near_hundred(93) → True
near_hundred(90) → True
near_hundred(89) → False