Popcorn Hack #1
score = 55
if score >= 65:
print("You passed the test!")
else:
print("You failed the test.")
You failed the test.
%%js
let score = 85;
// Conditional statement
if (score >= 65) {
console.log("You passed the test!");
} else {
console.log("You failed the test.");
}
<IPython.core.display.Javascript object>
Popcorn Hack #2
owns_a_key = True
if owns_a_key:
print("Come on in!")
else:
print("You need a key to come in here")
Come on in!
%%js
let owns_a_key = true;
if (owns_a_key) {
console.log("Come on in!");
} else {
console.log("You need a key to come in here.");
}
<IPython.core.display.Javascript object>
Popcorn hack 3
import random
roll_dice = random.randint(1, 6)
print("Rolling the dice...")
print(f"You rolled a {roll_dice}")
if roll_dice == 1:
print("Oh no! You rolled a one. Try again!")
elif roll_dice <= 3:
print("You rolled a low number. Better luck next time!")
elif roll_dice == 4:
print("Not bad! You rolled a four.")
elif roll_dice <= 5:
print("Great roll! You rolled a five.")
else:
print("Awesome! You rolled a six! You're on a roll!")
Rolling the dice...
You rolled a 6
Awesome! You rolled a six! You're on a roll!
%%js
let roll_dice = Math.floor(Math.random() * 6) + 1;
console.log("Rolling the dice...");
console.log(`You rolled a ${roll_dice}`);
if (roll_dice === 1) {
console.log("Oh no! You rolled a one. Try again!");
} else if (roll_dice <= 3) {
console.log("You rolled a low number. Better luck next time!");
} else if (roll_dice === 4) {
console.log("Not bad! You rolled a four.");
} else if (roll_dice === 5) {
console.log("Great roll! You rolled a five.");
} else {
console.log("Awesome! You rolled a six! You're on a roll!");
}
<IPython.core.display.Javascript object>
Homework 1
3.7
Popcorn Hack 1
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.")
You get a fruit snack.
Popcorn Hack 2
# 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.")
You don't have enough money to buy a laptop.
Popcorn Hack 3
%%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>
Homeowkr
age = int(input("Enter your age: "))
has_ball = input("Do you have a ball? (yes/no): ").lower()
can_join_game = age >= 5 and has_ball == "yes"
if can_join_game:
if age < 8:
message = "You can play with the younger kids."
else:
message = "You can play with the older kids."
print(f"Great! {message}")
else:
print(" you can't join the game yet.")
you can't join the game yet.