Skip to the content.

3.5 Hacks!

3.5 Popcorn and Homework Hacks

Popcorn Hack #1

# Function to check if a boolean is true
def check_boolean(value):
    if value:
        print("The boolean is True.")
    else:
        print("The boolean is False.")

# Input from the user
user_input = input("Enter a boolean value (True or False): ").strip().lower()

# Convert input to a boolean
if user_input == 'true':
    check_boolean(True)
elif user_input == 'false':
    check_boolean(False)
else:
    print("Invalid input. Please enter 'True' or 'False'.")
variableA = True
variableB = False

if variableA:
    print("VariableA is true!")
else:
    print("VariableA is false!")

if variableA and not variableB:
    print("VariableA is true and Variable B is false")

if variableA or variableB:
    print("Either variableA or variableB is true")    

Popcorn Hack #2

if number > 10:
    print("number is greater than ten!")
else:
    print("number is not greater than ten!")

Popcorn Hack #3

if number >= 100 and number <= 999:
    print("number is three digits!")
else:
    print("number is not three digits!")

Homework Hack

def truth_table():
    print("A\tB\tA AND B\tA OR B\tNOT A\tNOT B")
    print("-" * 40)
    
    # Iterate through all combinations of True (1) and False (0)
    for A in [True, False]:
        for B in [True, False]:
            A_and_B = A and B
            A_or_B = A or B
            not_A = not A
            not_B = not B
            
            # Print the results
            print(f"{A}\t{B}\t{A_and_B}\t\t{A_or_B}\t\t{not_A}\t{not_B}")

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

Output: A B NOT (A AND B) NOT A OR NOT B NOT (A OR B) NOT A AND NOT B ———————————————————— True True False False False False True False True True False False False True True True False False False False True True True True