Fall 2024 - P2
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.7-P2 Nested Conditionals
Student led teaching on Nested Conditionals using Javascript and Python
Content Outline
3.7.1
- If statements
- Else Statements
- Nested Conditionals
- Examples using Javascript and Python
3.7.2
- Homework Hack Assignment
- Homework Hack Javscript and Python Examples
Popcorn Hack #1
Task:
Prompt the user for their age and whether they like sweets, then based on their age and preference, print either “You can have candy,” “You can have a healthy snack,” or “You get a fruit snack.”
age = int(input("Enter your age: "))
likes_sweets = input("Do you like sweets? (yes/no): ").lower()
if age >= 10:
if likes_sweets == "yes":
print("You can have candy!")
else:
print("You can have a healthy snack.")
else:
print("You get a fruit snack.")
Popcorn Hack #2
Task
Create a Python program that checks your savings against the prices of different laptops and informs you which one you can afford to buy.
# Savings
savings = 500
# Laptop prices
dell_price = 800
hp_price = 700
macbook_price = 1200
# Determine which laptop you can buy
if savings >= macbook_price:
print("You can buy a MacBook!")
elif savings >= dell_price:
print("You can buy a Dell laptop!")
elif savings >= hp_price:
print("You can buy an HP laptop!")
else:
print("You don't have enough money to buy a laptop.")
<IPython.core.display.Javascript object>
Popcorn Hack #3
Task
Create a simple JavaScript program that on the topic of your choice using boolean values and nested conditionals
%%js
// Grocery Conditions
let is_store_open = true;
let is_vegetables_available = false;
// Shopping logic based on store and item availability
if (is_store_open) {
console.log("You can go grocery shopping.");
if (is_vegetables_available) {
console.log("Buy some fresh vegetables.");
} else {
console.log("Check for other items on your list.");
}
} else {
console.log("The store is closed, shop another day.");
}