Summary | 3.1 | 3.2 | 3.3 | 3.4 | 3.5 | 3.6 | 3.7 | 3.10 | Relevancy |
Homeworks and Popcorn Hacks for Lessons
Homeworks and Hacks for Lesson 3.6
Lesson 3.6 Popcorn Hacks and Homework
Popcorn Hack #1
Javascript Version
%%js
let teamScore = 3;
let opponentScore = 2;
if (teamScore > opponentScore) {
console.log("Your team is winning!");
} else {
console.log("Your team is not winning, keep pushing!");
}
<IPython.core.display.Javascript object>
Python Version
team_score = 3
opponent_score = 2
if team_score > opponent_score:
print("Your team is winning!")
else:
print("Your team is not winning, keep pushing!")
Your team is winning!
Popcorn Hack #2
Javascript Version
%%js
let userInput = prompt("Did your team win the soccer game? (yes/no)").toLowerCase();
let teamWon = (userInput === "yes");
if (teamWon) {
console.log("Congratulations! Your team won the game.");
} else {
console.log("Better luck next time. Keep working hard!");
}
<IPython.core.display.Javascript object>
Python Version
user_input = input("Did your team win the soccer game? (yes/no): ").lower()
team_won = (user_input == "yes")
if team_won:
print("Congratulations! Your team won the game.")
else:
print("Better luck next time. Keep working hard!")
Congratulations! Your team won the game.
Popcorn Hack #3
Javascript Version
%%js
let prizes = ["a brand new car", "a vacation trip", "a free meal", "a gift card", "a laptop", "nothing"];
let prizeIndex = Math.floor(Math.random() * prizes.length);
console.log("Spinning the wheel of fortune...");
let selectedPrize = prizes[prizeIndex];
console.log(`Congratulations! You won ${selectedPrize}!`);
if (selectedPrize === "nothing") {
console.log("Oh no! Better luck next time.");
} else if (selectedPrize === "a brand new car") {
console.log("Wow! You hit the jackpot!");
} else if (selectedPrize === "a vacation trip") {
console.log("Pack your bags! You're going on a vacation!");
} else if (selectedPrize === "a free meal") {
console.log("Enjoy your free meal at a fancy restaurant.");
} else if (selectedPrize === "a gift card") {
console.log("You got a gift card! Time to go shopping.");
} else {
console.log("Awesome! A new laptop to boost your productivity.");
}
<IPython.core.display.Javascript object>
Python Version
import random
prizes = ["a brand new car", "a vacation trip", "a free meal", "a gift card", "a laptop", "nothing"]
prize_index = random.randint(0, len(prizes) - 1)
print("Spinning the wheel of fortune...")
selected_prize = prizes[prize_index]
print(f"Congratulations! You won {selected_prize}!")
if selected_prize == "nothing":
print("Oh no! Better luck next time.")
elif selected_prize == "a brand new car":
print("Wow! You hit the jackpot!")
elif selected_prize == "a vacation trip":
print("Pack your bags! You're going on a vacation!")
elif selected_prize == "a free meal":
print("Enjoy your free meal at a fancy restaurant.")
elif selected_prize == "a gift card":
print("You got a gift card! Time to go shopping.")
else:
print("Awesome! A new laptop to boost your productivity.")
Spinning the wheel of fortune...
Congratulations! You won a brand new car!
Wow! You hit the jackpot!
Homeowork 3.6
Python Version
import random
questions = [
{
"question": "What is the largest planet in our solar system?",
"options": ["A) Earth", "B) Jupiter", "C) Mars", "D) Saturn"],
"answer": "B"
},
{
"question": "What is the closest star to Earth?",
"options": ["A) Proxima Centauri", "B) Alpha Centauri", "C) Betelgeuse", "D) Sirius"],
"answer": "A"
},
{
"question": "Which planet is known as the Red Planet?",
"options": ["A) Venus", "B) Earth", "C) Mars", "D) Jupiter"],
"answer": "C"
},
{
"question": "What is the name of our galaxy?",
"options": ["A) Andromeda", "B) Milky Way", "C) Triangulum", "D) Whirlpool"],
"answer": "B"
},
{
"question": "How many planets are in our solar system?",
"options": ["A) 7", "B) 8", "C) 9", "D) 10"],
"answer": "B"
}
]
def ask_question(question_data):
"""Function to ask a single question and return if the answer is correct."""
print(question_data["question"])
for option in question_data["options"]:
print(option)
answer = input("Your answer (A/B/C/D): ").strip().upper()
if answer == question_data["answer"]:
print("Correct!\n")
return True
else:
print(f"Wrong! The correct answer was {question_data['answer']}\n")
return False
def quiz():
"""Main function to run the quiz."""
print("Welcome to the Space and Astronomy Quiz!\n")
score = 0
random.shuffle(questions)
for question_data in questions:
if ask_question(question_data):
score += 1
print(f"Quiz over! Your score: {score}/{len(questions)}")
if __name__ == "__main__":
quiz()
Welcome to the Space and Astronomy Quiz!
Which planet is known as the Red Planet?
A) Venus
B) Earth
C) Mars
D) Jupiter
Correct!
What is the name of our galaxy?
A) Andromeda
B) Milky Way
C) Triangulum
D) Whirlpool
Correct!
What is the largest planet in our solar system?
A) Earth
B) Jupiter
C) Mars
D) Saturn
Correct!
What is the closest star to Earth?
A) Proxima Centauri
B) Alpha Centauri
C) Betelgeuse
D) Sirius
Correct!
How many planets are in our solar system?
A) 7
B) 8
C) 9
D) 10
Correct!
Quiz over! Your score: 5/5