3.7
Popcorn Hacks
3.7.1 Nested Conditionals Python
Hack 1
Try adding an additional condition for hiking (like location_determined or miles_in_the_hike).
energy = "low"
weather = "sunny"
transportation = "available"
boots = "not present"
print("Your energy is " + energy)
print("The weather is " + weather)
print("Your transportation is " + transportation)
print("Your boots are " + boots)
if energy == "high":
if weather == "sunny":
if transportation == "available":
if boots == "present":
print("You are ready to go hiking!")
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.")
else:
print("You don't have enough energy to go hiking.")
Your energy is low
The weather is sunny
Your transportation is available
Your boots are not present
You don't have enough energy to go hiking.
I added a condition for energy levels.
3.7.2 Nested Conditionals JavaScript
Hack 1
%%html
<script>
let energy = "low";
let weather = "sunny";
let transportation = "available";
let boots = "not present";
console.log("Your energy is " + energy);
console.log("The weather is " + weather);
console.log("Your transportation is " + transportation);
console.log("Your boots are " + boots);
if (energy === "high") {
if (weather === "sunny") {
if (transportation === "available") {
if (boots === "present") {
console.log("You are ready to go hiking!");
} 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.");
}
} else {
console.log("You don't have enough energy for hiking.");
}
</script>
I added a condition for energy levels again.
Hacks
3.7.3 Nested Conditionals Python
Hack 1
Write Python pseudocode to decide whether or not to go to the beach.
- The weather must be sunny.
- You must have sunscreen.
- You must have enough snacks.
- If all conditions are met, output a message that says you are ready for the beach.
- If you don’t have enough snacks, suggest getting snacks first.
- If you don’t have sunscreen, suggest buying sunscreen.
- If the weather is not sunny, output that it’s not a good day for the beach.
Set weather1 to "sunny"
Set sunscreen1 to True
Set snacks1 to 3
Function beach_check(weather, sunscreen, snacks):
If weather is "sunny":
If sunscreen is True:
If snacks is greater than or equal to 5:
Print "You can go to the beach!"
Else:
Print "You need more snacks"
Else:
Print "Get some sunscreen"
Else:
Print "Today's not a good day"
Call beach_check with weather1, sunscreen1, and snacks1
Expected Output: "You need more snacks"
Hack 2
Write a Python code that checks if you can adopt a pet. Include the following conditions:
- You must be 18 years or older.
- You must have enough space in your home (more than 50 square feet).
- You must be available to take care of the pet (true/false).
- If all conditions are true, print that you can adopt the pet.
- If you don’t have enough space, print that you need a bigger home.
- If you aren’t available to take care of the pet, print that you need to make time.
- If you are not 18 or older, print that you must be at least 18 to adopt a pet.
age1 = 18
space1 = 100
available1 = False
print(f"Your age is {age1}")
print(f"You have {space1} square feet in your house")
print(f"Do you have availability? {available1}\n")
def pet_check(age, space, available):
if age>=18:
if space>=50:
if available==True:
print("You can adopt a pet!")
else:
print("You need to make time for a pet.")
else:
print("You need a bigger home.")
else:
print("You need to be at least 18 to adopt a pet.")
pet_check(age1, space1, available1)
Your age is 18
You have 100 square feet in your house
Do you have availability? False
You need to make time for a pet.
Hack 3
Write Python code to determine whether or not to participate in a marathon.
Check the following conditions:
- The weather must be clear.
- You must have running shoes.
- You must have practiced for at least 10 days.
- If all conditions are met, print that you are ready for the marathon.
- If you don’t have running shoes, print that you need to buy shoes first.
- If you haven’t practiced enough, print that you need to practice more.
- If the weather is not clear, print that it’s not the right time for the marathon.
weather1 = "clear"
running_shoes1 = True
days_practiced1 = 7
print(f"The weather is {weather1}")
print(f"Do you have your shoes? {running_shoes1}")
print(f"You have practiced for {days_practiced1} days\n")
def marathon_check(weather, running_shoes, days_practiced):
if weather == "clear":
if running_shoes == True:
if days_practiced>=10:
print("You are ready to run a marathon!")
else:
print(f"Practice for at least {10-days_practiced} more days.")
else:
print("You should get your running shoes.")
else:
print("Pick a different day to run a marathon.")
marathon_check(weather1, running_shoes1, days_practiced1)
The weather is clear
Do you have your shoes? True
You have practiced for 7 days
Practice for at least 3 more days.
3.7.4 Nested Conditionals JavaScript
Hack 1
Write Javascript pseudocode to decide whether or not to study for an exam.
Check the following conditions:
- You must have your study materials (books, notes, etc.).
- You must have a quiet place to study.
- You must not be feeling too tired.
- If all conditions are met, print that you are ready to study.
- If you don’t have a quiet place, suggest finding a quiet location.
- If you are too tired, suggest taking a nap or resting first.
- If you don’t have your study materials, print that you need to gather them first.
Set studyMaterials1 to "present"
Set locationLoud1 to false
Set tiredness1 to 19
Function studyNow(studyMaterials, locationLoud, tiredness):
If studyMaterials is "present":
If locationLoud is false:
If tiredness is less than or equal to 10:
Print "You are ready to study!"
Else:
Print "You are too tired. Take a nap or rest first."
Else:
Print "You need to find a quiet place to study."
Else:
Print "You need to gather your study materials first."
Call studyNow with studyMaterials1, locationLoud1, and tiredness1
Expected Output: "You are too tired. Take a nap or rest first."
Hack 2
Write Javascript code deciding whether or not you can bake a cake.
Check the following conditions:
- You must have all the ingredients (flour, eggs, sugar).
- Your oven must be working.
- You must have at least 2 hours to bake and cool the cake.
- If all conditions are true, print that you can start baking.
- If you don’t have enough time, print that you need to find more time.
- If the oven is not working, print that you need to fix or replace the oven.
- If you don’t have all the ingredients, print what you are missing.
%%html
<script>
const requiredIngredients = ["flour", "eggs", "sugar"];
ingredients1 = ["flour", "eggs", "sugar"];
ovenWorking1 = true;
hoursAvailable1 = 2;
console.log(`You have ${ingredients1}.`)
console.log(`Is the oven working? ${ovenWorking1}`)
console.log(`You have ${hoursAvailable1} hours available.\n`)
function bakeCheck(ingredients, ovenWorking, hoursAvailable){
if (requiredIngredients.every(ingredient => ingredients.includes(ingredient))){
if (ovenWorking == true){
if (hoursAvailable>=2){
console.log("You can start baking a cake!");
} else {
console.log("You need to find more time to bake.");
}
}else {
console.log("You need to fix or replace the oven.");
}
} else {
// Find missing ingredients
const missingIngredients = requiredIngredients.filter(ingredient => !ingredients.includes(ingredient));
console.log(`You are missing ${missingIngredients.join(", ")}`);
}
}
bakeCheck(ingredients1, ovenWorking1, hoursAvailable1);
</script>
Hack 3
Write Javascript code to determine whether or not to go camping.
Check the following conditions:
- The weather must be clear.
- You must have a tent.
- You must have enough food and water for the trip.
- If all conditions are met, print that you are ready to go camping.
- If you don’t have a tent, print that you need to buy or borrow a tent.
- If you don’t have enough food and water, print that you need to pack more.
- If the weather is not clear, print that it’s not a good time for camping.
%%html
<script>
//test var
let weather1 = "clear";
let hasTent1 = false;
let foodAndWater1 = 5;
console.log(`The weather is ${weather1}.`);
console.log(`Do you have a tent? ${hasTent1}`);
console.log(`You have ${foodAndWater1} units of food and water.\n`);
function goCamping(weather, hasTent, foodAndWater) {
if (weather === "clear") {
if (hasTent) {
if (foodAndWater >= 10) {
console.log("You are ready to go camping!");
} else {
console.log("You need to pack more food and water.");
}
} else {
console.log("You need to buy or borrow a tent.");
}
} else {
console.log("It's not a good time for camping.");
}
}
//print statement
goCamping(weather1, hasTent1, foodAndWater1);
</script>