Skip to the content.

Group Lesson 5 Homework

Popcorn and Homework Hacks

Popcorn Hacks

BMI = 30

# Conditional statement
if BMI >= 30:
    print("You are obese!")
else:
    print("You are not obese")
You are obese!
%%js
let BMI = 30;

// Conditional statement
if (BMI >= 75) {
    console.log("You passed the test!");
} else {
    console.log("You failed the test.");
}
<IPython.core.display.Javascript object>
isEighteen = True

if isEighteen:
    print("Can vote.")
else:
    print("Cannot vote.")
Can vote.
%%js
let isEighteen = True;

if (isEighteen) {
    console.log("Can vote.");
} else {
    console.log("Cannot vote.");
}

<IPython.core.display.Javascript object>
import random
lottery_answer = random.randint(0, 20)
lottery_roll = random.randint(0,20)

if lottery_answer == lottery_roll:
    print("You rich! You won the lottery! 🤑🤑🤑")
else:
    print("Your roll is:", lottery_roll)
    print("The winning number was:", lottery_answer)
    print("You are still poor! 😔")
Your roll is: 8
The winning number was: 12
You are still poor! 😔
%%js
let lottery_answer = Math.floor(Math.random() * 20) + 1;
let lottery_roll = Math.floor(Math.random() * 20) + 1;

if (lottery_answer === lottery_roll) {
    console.log("You rich! You won the lottery! 🤑🤑🤑");
} else {
    console.log("Your roll is:", lottery_roll);
    console.log("The winning number was:", lottery_answer);
    console.log("You are still poor! 😔")
}
<IPython.core.display.Javascript object>
age = int(input("Enter your age: "))
likes_alcohol = input("Do you like alcohol? (yes/no): ").lower()

if age >= 21:
    if likes_alcohol == "yes":
        print("You can have beer!")
    else:
        print("You can have a non-alcoholic drink.")
else:
    print("You get water.")
You can have another drink.
%%js
let age = 22
let likes_alcohol = true //prompt doesn't work

if (age >= 21) {
    if (likes_alcohol) {
        console.log("You can have beer");
    } else {
        console.log("You can have a non-alcoholic drink");
    }
} else {
    console.log("You can get water.")
}
<IPython.core.display.Javascript object>
savings = 500

# Laptop prices
bicycle = 200
ebike = 2000
scooter = 75

# Determine which laptop you can buy
if savings >= bicycle:
    print("You can buy a bike 🚲!")
elif savings >= ebike:
    print("You can buy an ebike 🏍️!")
elif savings >= scooter:
    print("You can buy a scooter 🛴!")
else:
    print("You don't have enough money to buy anything.")
%%js
// Grocery Conditions
let is_school_open = false;
let is_homework_assigned = false;

// Shopping logic based on store and item availability
if (is_store_open) {
    console.log("Go to school.");

    if (is_homework_assigned) {
        console.log("Do your homework.");
    } else {
        console.log("Do whatever your want.");
    }
} else {
    console.log("Go on a holiday.");
}
<IPython.core.display.Javascript object>

Homework Hacks

Added speed based bonuses for the quiz and improved code efficiency with loop and lists.
import time
questions = ["What is the acronym of the city has the empire state building?", "What city has the space needle?", "What city has a well known arch? (no spaces in answer)", "wWhat city has the willis tower?", "What city has the golden gate bridge?"]
answers = ["nyc", "seattle", "st.louis", "chicago", "san francisco"]

score = 0

for i in range(len(questions)):
    start_time = time.time()
    answer = input(questions[i])
    time_taken = time.time() - start_time
    if time_taken < 3:
        print(f"You scored 2 points for being fast. And your score is {score}")
        if answer.strip().lower() == answers[i]:
            score += 2
            print(f"Correct! And your score is {score}")
    else:
        if answer.strip().lower() == answers[i]:
            score += 1
            print(f"Correct! And your score is {score}")

print("Final score", score)
You scored 2 points for being fast. And your score is 0
Correct! And your score is 2
You scored 2 points for being fast. And your score is 2
Correct! And your score is 4
You scored 2 points for being fast. And your score is 4
Correct! And your score is 6
You scored 2 points for being fast. And your score is 6
Correct! And your score is 8
You scored 2 points for being fast. And your score is 8
Correct! And your score is 10
Final score 10
age = int(input("Enter your age: "))
ball = input("Do you have a ball (yes or no)?")

if age > 5 and ball == "yes":
    print("You can play the game! And..")
    if age > 13:
        print("You can play with the older kids! 🧔")
    else:
        print("Play with the younger kids! 👦")
else:
    print("Sorry you can't join the game yet 😭")

You can play the game! And..
You can play with the older kids! 🧔