Python Popcorn and Homework Hacks

#Popcorn Hack 1

number = 4

if number < 0:
    print("Number is negative")
elif number == 0:
    print("Number is 0")
else:
    print("Number is Positive")
Number is Positive
#Popcorn Hack 3

char = "e"

if char in "aeiou":
    print ("Character is a vowel!")
else:
    print("Character is a consonant.")
Character is a vowel!
# Homework Hack 1

def truth_table():
    print(f"A\tB\tC\tA AND (B OR C)")
    print("-" * 30)
    for A in [True, False]:
        for B in [True, False]:
            for C in [True, False]:
                result = A and (B or C)
                print(f"{A}\t{B}\t{C}\t{result}")
truth_table()
A	B	C	A AND (B OR C)
------------------------------
True	True	True	True
True	True	False	True
True	False	True	True
True	False	False	False
False	True	True	False
False	True	False	False
False	False	True	False
False	False	False	False
# Homework Hack 2

import random

def de_morgan_check(A, B):
    # not (A and B) == not A or not B
    return not (A and B) == (not A or not B)

# Function for a Yes/No input
def get_yes_no_input(prompt):
    while True:
        choice = input(prompt + " (yes/no): ").lower()
        if choice in ["yes", "no"]:
            return choice == "yes"
        else:
            print("Invalid input! Please enter 'yes' or 'no'.")

def de_morgan_game():
    print("De Morgan's Law Game")
    A = random.choice([True, False])
    B = random.choice([True, False])
    
    player_A = get_yes_no_input("Do you want to choose option A?")
    player_B = get_yes_no_input("Do you want to choose option B?")
    
    if de_morgan_check(player_A, player_B):
        print("Congratulations! You won using De Morgan's Law!")
    else:
        print("Sorry, you lost! Try again.")
    
    # Show the hidden values for A and B
    print(f"Hidden values were: A = {A}, B = {B}")
    
# Start the game
de_morgan_game()


De Morgan's Law Game
Congratulations! You won using De Morgan's Law!
Hidden values were: A = True, B = True

Javascript Popcorn and Homework Hacks

%%javascript

/* Popcorn Hack 2 */

num_1 = 83
num_2 = 34

if (num_1 > num_2) {
    console.log(num_1 + " is bigger than " + num_2);
} else if (num_1 < num_2) {
    console.log(num_2 + " is bigger than " + num_1);
} else {
    console.log(num_1 + " is equal to " + num_2);
}
<IPython.core.display.Javascript object>
%%javascript

/* Popcorn Hack 3 */

number = 7
 
if (number % 2 === 0) {
    console.log(number + " is even.");
} else {
   console.log(number + " is odd.");
}
<IPython.core.display.Javascript object>
%%javascript

/* Homework Hack 1 */

password = prompt("Enter a password:")

if (password.length >= 10) {
    console.log("Length requirement met.")
}
else {
    console.log("Longer length required")
    exit
}
    
function validatePassword(password) {
    const hasUpperCase = /[A-Z]/.test(password);    // Contains uppercase letters
    const hasLowerCase = /[a-z]/.test(password);    // Contains lowercase letters
    const hasNumber = /\d/.test(password);          // Contains numbers
    const hasNoSpaces = !/\s/.test(password);       // Does not contain spaces

    // All conditions must be true
    return hasUpperCase && hasLowerCase && hasNumber && hasNoSpaces;
    
}

// Function to create a password and check if it's valid
function createPassword() {
    if (validatePassword(password)) {
        alert("Password is valid!");
    } else {
        alert("Invalid password! \nMake sure it is at least 10 characters long, includes uppercase and lowercase letters, has at least one number, and contains no spaces.");
    }
}

// Start the password creation process
createPassword();
<IPython.core.display.Javascript object>
%%javascript

/* Homework Hack 2 */

// Function to start the personality quiz
function startQuiz() {
    let score = 0;

    // Questions and choices
    let questions = [
        { question: "1. Favorite color?", options: ["a) Red", "b) Blue", "c) Green"], points: [1, 2, 3] },
        { question: "2. Free time?", options: ["a) Reading", "b) Outdoors", "c) Socializing"], points: [2, 3, 1] },
        { question: "3. Which pet?", options: ["a) Dog", "b) Cat", "c) Bird"], points: [1, 2, 3] },
        { question: "4. Ideal vacation?", options: ["a) Beach", "b) Mountains", "c) City"], points: [1, 3, 2] },
        { question: "5. Fav movie genre?", options: ["a) Action", "b) Comedy", "c) Drama"], points: [1, 2, 3] },
        { question: "6. Night owl?", options: ["a) Yes", "b) No", "c) Sometimes"], points: [3, 2, 1] },
        { question: "7. Stress busting strat?", options: ["a) Deep breathing", "b) No strat!", "c) Distract myself"], points: [1, 3, 2] },
        { question: "8. Favorite season?", options: ["a) Summer", "b) Winter", "c) Fall"], points: [1, 2, 3] },
        { question: "9. Decision making strat?", options: ["a) Logic", "b) Emotion", "c) Instinct"], points: [1, 2, 3] },
        { question: "10. Colaborate or no?", options: ["a) Yes", "b) No", "c) Depends"], points: [1, 2, 3] }
    ];
    for (let i = 0; i < questions.length; i++) {
        let answer = prompt(questions[i].question + "\n" + questions[i].options.join("\n")).toLowerCase();
    
        if (answer === "a") {
            score += questions[i].points[0];
        } else if (answer === "b") {
            score += questions[i].points[1];
        } else if (answer === "c") {
            score += questions[i].points[2];
        }
    }

    if (score <= 15) {
        alert("Your personality type is: Outdoorsy. You touch grass very often");
    } else if (score <= 22) {
        alert("Your personality type is: Intellectual. Have fun reading that textbook");
    } else {
        alert("Your personality type is: Whimsical. You like clouds probably");
    }
}

startQuiz();


<IPython.core.display.Javascript object>