Skip to the content.

Homeworks 3.7

## Popcorn Hack 1 Python 3.7.1

# Initial conditions
weather = "sunny"
transportation = "available"
boots = "not present"
location_determined = False  # New condition

print("The weather is " + weather)
print("Your transportation is " + transportation)
print("Your boots are " + boots)
print("Location determined: " + str(location_determined))

# Hiking readiness checks
if weather == "sunny":
    if transportation == "available":
        if boots == "present":
            if location_determined:  # Check if the location is determined
                print("You are ready to go hiking!")
            else:
                print("You need to determine a hiking location first.")
        else:
            print("You need to find your boots first.")
    else:
        print("You need to arrange transportation.")
else:
    print("It's not good weather for hiking.")


The weather is sunny
Your transportation is available
Your boots are not present
Location determined: False
You need to find your boots first.

Javascript Popcorn Hack 1 3.7.2

Popcorn Hack 1 Try adding an additional condition for hiking (like location_determined or miles_in_the_hike).

%%js
let weather = "sunny";
let transportation = "available";
let boots = "not present";
let location_determined = "yes"; // New condition for hiking location

console.log("The weather is " + weather);
console.log("Your transportation is " + transportation);
console.log("Your boots are " + boots);
console.log("Hiking location determined: " + location_determined);

if (weather === "sunny") {
    if (transportation === "available") {
        if (boots === "present") {
            if (location_determined === "yes") {
                console.log("You are ready to go hiking!");
            } else {
                console.log("You need to determine your hiking location.");
            }
        } else {
            console.log("You need to find your boots first.");
        }
    } else {
        console.log("You need to arrange transportation.");
    }
} else {
    console.log("It's not good weather for hiking.");
}

<IPython.core.display.Javascript object>

##3.7.3 Nested Conditionals Python Hack 1 #Hack 1: Write Python pseudocode to decide whether or not to go to the beach.

Pseudocode for deciding whether or not to go to the beach

1. Define function decideBeachTrip with parameters:
   - weatherIsSunny
   - hasSunscreen
   - hasEnoughSnacks

2. If weatherIsSunny is True:
     a. If hasSunscreen is True:
         i. If hasEnoughSnacks is True:
             - Print "You are ready for the beach!"
         ii. Else (if hasEnoughSnacks is False):
             - Print "You need to get snacks first."
     b. Else (if hasSunscreen is False):
         - Print "You need to buy sunscreen."
   
3. Else (if weatherIsSunny is False):
     - Print "It's not a good day for the beach."

4. End function

# Example usage:
Call decideBeachTrip with different values for weatherIsSunny, hasSunscreen, and hasEnoughSnacks.
## 3.7.3 Nested Conditionals Python Hack 2
#Hack 2: Write a Python code that checks if you can adopt a pet.

def checkAdoptionEligibility(age, space, available):
    if age >= 18:
        if space > 50:
            if available:
                print("You can adopt the pet!")
            else:
                print("You need to make time to take care of the pet.")
        else:
            print("You need a bigger home to adopt a pet.")
    else:
        print("You must be at least 18 to adopt a pet.")

# Example usage
checkAdoptionEligibility(20, 60, True)   # Output: You can adopt the pet!
checkAdoptionEligibility(16, 60, True)   # Output: You must be at least 18 to adopt a pet.
checkAdoptionEligibility(20, 40, True)   # Output: You need a bigger home to adopt a pet.
checkAdoptionEligibility(20, 60, False)  # Output: You need to make time to take care of the pet.

You can adopt the pet!
You must be at least 18 to adopt a pet.
You need a bigger home to adopt a pet.
You need to make time to take care of the pet.
## 3.7.3 Nested Conditionals Python Hack 3
#Hack 3: Write Python code to determine whether or not to participate in a marathon.

def checkMarathonParticipation(weather, running_shoes, practice_days):
    if weather == "clear":
        if running_shoes:
            if practice_days >= 10:
                print("You are ready for the marathon!")
            else:
                print("You need to practice more.")
        else:
            print("You need to buy running shoes first.")
    else:
        print("It's not the right time for the marathon.")

# Example usage
checkMarathonParticipation("clear", True, 12)    # Output: You are ready for the marathon!
checkMarathonParticipation("clear", False, 12)   # Output: You need to buy running shoes first.
checkMarathonParticipation("clear", True, 5)     # Output: You need to practice more.
checkMarathonParticipation("rainy", True, 12)    # Output: It's not the right time for the marathon.
You are ready for the marathon!
You need to buy running shoes first.
You need to practice more.
It's not the right time for the marathon.
## Python Quiz Question 1 
# Answer is C- you are too young to enter
##Python Quiz Question 2
# Answer is B- x is positive but y is not
##Python Quiz Question 3 
# Answer is C- It's too cold

3.7.4 Nested Conditionals Javascript Hack 1

Pseudocode for checking if you’re ready to study

// Pseudocode for checking if you're ready to study

1. Define function checkStudyReadiness with parameters:
   - studyMaterials
   - quietPlace
   - feelingTired

2. If studyMaterials is true:
     a. If quietPlace is true:
         i. If feelingTired is false:
             - Output "You are ready to study!"
         ii. Else (if feelingTired is true):
             - Output "You should take a nap or rest first."
     b. Else (if quietPlace is false):
         - Output "You need to find a quiet location."

3. Else (if studyMaterials is false):
     - Output "You need to gather your study materials first."

4. End function

# Example usage:
Call checkStudyReadiness with different values for studyMaterials, quietPlace, and feelingTired.

Hack 2: Write Javascript code deciding whether or not you can bake a cake.

%%js
// Define a function to check if you can bake a cake
function checkBakingReadiness(ingredients, ovenWorking, availableTime) {
    // Check if all ingredients are present
    let missingIngredients = [];
    if (!ingredients.flour) {
        missingIngredients.push("flour");
    }
    if (!ingredients.eggs) {
        missingIngredients.push("eggs");
    }
    if (!ingredients.sugar) {
        missingIngredients.push("sugar");
    }

    // Check if you have all ingredients
    if (missingIngredients.length > 0) {
        console.log("You are missing: " + missingIngredients.join(", ") + ".");
    } else {
        // Check if the oven is working
        if (!ovenWorking) {
            console.log("You need to fix or replace the oven.");
        } else {
            // Check if you have at least 2 hours
            if (availableTime >= 2) {
                console.log("You can start baking!");
            } else {
                console.log("You need to find more time.");
            }
        }
    }
}

// Example usage:
const ingredients = {
    flour: true,
    eggs: true,
    sugar: true
};

checkBakingReadiness(ingredients, true, 2); // Output: You can start baking!
checkBakingReadiness(ingredients, true, 1); // Output: You need to find more time.
checkBakingReadiness(ingredients, false, 2); // Output: You need to fix or replace the oven.
checkBakingReadiness({ flour: true, eggs: false, sugar: true }, true, 2); // Output: You are missing: eggs.

<IPython.core.display.Javascript object>

Hack 3: Write Javascript code to determine whether or not to go camping.

%%js
// Define a function to check if you are ready to go camping
function checkCampingReadiness(weatherClear, hasTent, enoughFoodAndWater) {
    // Check if the weather is clear
    if (!weatherClear) {
        console.log("It's not a good time for camping.");
    } else {
        // Check if you have a tent
        if (!hasTent) {
            console.log("You need to buy or borrow a tent.");
        } else {
            // Check if you have enough food and water
            if (!enoughFoodAndWater) {
                console.log("You need to pack more food and water.");
            } else {
                // All conditions met
                console.log("You are ready to go camping!");
            }
        }
    }
}

// Example usage:
checkCampingReadiness(true, true, true);  // Output: You are ready to go camping!
checkCampingReadiness(true, false, true); // Output: You need to buy or borrow a tent.
checkCampingReadiness(true, true, false); // Output: You need to pack more food and water.
checkCampingReadiness(false, true, true); // Output: It's not a good time for camping.

<IPython.core.display.Javascript object>