3.7 Popcorn Hacks
3.7 Nested Conditionals
Popcorn Hack 1 in Python
grade = int(input("Enter your grade level: "))
outside_activity = input("Do you like to go outside by yourself? (yes/no): ").lower()
if grade >= 8:
if outside_activity == "yes":
print("You can go outside")
else:
print("Fine, you can stay home")
else:
print("You're too young. You can't go outside by yourself")
You can go outside
Popcorn Hack 1 in Javascript
%%js
let grade = int(input("Enter your grade level: "))
outside_activity = input("Do you like to go outside by yourself? (yes/no): ").lower()
if grade >= 8:
if outside_activity == "yes":
print("You can go outside")
else:
print("Fine, you can stay home")
else:
print("You're too young. You can't go outside by yourself")
<IPython.core.display.Javascript object>
Popcorn Hack 2 in Python
# Savings
savings = 1000
# Laptop prices
dell_price = 1200
hp_price = 900
macbook_price = 1500
# 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.")
You can buy an HP laptop!
Popcorn Hack 3 in Javascript
%%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.");
}
<IPython.core.display.Javascript object>