Skip to the content.

3.5 Homework

hw for 3.5 lesson

Popcorn #1

temperature = 80

#Boolean expression:
cold = temperature < 65

#Output:
if cold:
  print("It's cold")

else:
  print("It's not cold")

let temperature = 42;

// Boolean expression:
let cold = temperature < 65;

// Output:
if (cold) {
    console.log("It's cold");
} else {
    console.log("It's not cold");
}

Popcorn Hack #2

let temperature = 100;

// Boolean expressions
let comfortable = temperature < 83 && temperature > 63;
let notComfortable = temperature < 63 || temperature > 83;

if (comfortable) {
    console.log("It is a comfortable temperature");
} else if (notComfortable) {
    console.log("It is not a comfortable temperature");
}
## Example 1: Temperature Check
temperature = 43 

# Boolean expressions
comfortable = temperature < 83 and temperature > 63
not_comfortable = temperature < 63 or temperature > 83

if comfortable: 
    print("It is a comfortable temperature")
elif not_comfortable:
    print("It is not a comfortable temperature")

Popcorn Hack #3

// Define user status
let isAuthorized = True;  // Changed to true

// Use NOT to determine access
let canEnter = !isAuthorized;

// Output the result
console.log(canEnter ? "Access granted!" : "Access denied!");
# Define user status
is_authorized = True  # Changed to true

# Use NOT to determine access
can_enter = not is_authorized

# Output the result
print("Access granted!" if can_enter else "Access denied!")

Homework

def truth_table():
    # Define the values for x and y
    values = [2, 4]
    
    # Print the header of the truth table
    print(f"{'x':<5} {'y':<5} {'x AND y':<10} {'x OR y':<10}")
    
    # Iterate through all combinations of x and y
    for x in values:
        for y in values:
            # Define the AND operation
            if x == y:
                and_result = x
            else:
                and_result = 0  # Return 0 if x and y are not equal
            
            # Define the OR operation (sum in this case)
            or_result = x + y
            
            # Print the results
            print(f"{x:<5} {y:<5} {and_result:<10} {or_result:<10}")

# Call the function to display the truth table
truth_table()

x     y     x AND y    x OR y    
2     2     2          4         
2     4     0          6         
4     2     0          6         
4     4     4          8