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.7
Lesson 3.7 Popcorn Hacks and Homework
Popcorn Hack #1
Javascript Version
%%js
let age = parseInt(prompt("Enter your age:"));
let likesDrive = prompt("Can you legally drive? (yes/no):").toLowerCase();
if (age >= 16) {
if (likesDrive === "yes") {
console.log("You can legally drive!");
} else {
console.log("You cannot legally drive.");
}
} else {
console.log("You get to be a passenger.");
}
<IPython.core.display.Javascript object>
Python Version
age = int(input("Enter your age: "))
likes_drive = input("Can you legally drive? (yes/no): ").lower()
if age >= 16:
if likes_drive == "yes":
print("You can legally drive!")
else:
print("You can not legally drive.")
else:
print("You get to be a passenger.")
You can not legally drive.
Popcorn Hack #2
Python Version
laptops = {
"Dell XPS 13": 1200,
"MacBook Air": 999,
"HP Spectre x360": 1100,
"Lenovo ThinkPad X1": 1300,
"Acer Swift 3": 650
}
savings = float(input("Enter your total savings: $"))
affordable_laptops = []
for laptop, price in laptops.items():
if savings >= price:
affordable_laptops.append(laptop)
if affordable_laptops:
print("You can afford the following laptops:")
for laptop in affordable_laptops:
print(f"- {laptop}")
else:
print("Unfortunately, you cannot afford any of these laptops with your current savings.")
You can afford the following laptops:
- Dell XPS 13
- MacBook Air
- HP Spectre x360
- Lenovo ThinkPad X1
- Acer Swift 3
Popcorn Hack #3
Javascript Version
%%js
let teamWinning = true;
let scoredGoal = true;
if (teamWinning) {
console.log("Your team is winning!");
if (scoredGoal) {
console.log("Great job! Your team has scored a goal!");
} else {
console.log("Your team is winning, but they haven't scored yet. Keep it up!");
}
} else {
console.log("Your team is not winning.");
if (scoredGoal) {
console.log("At least your team has scored a goal. Keep fighting!");
} else {
console.log("Unfortunately, your team has not scored yet. Better luck next time!");
}
}
<IPython.core.display.Javascript object>
Homework 3.7
Python Version
def check_eligibility():
age = input("Please enter your age: ")
try:
age = int(age)
if age < 0:
print("Please enter a valid age.")
return
except ValueError:
print("Please enter a valid age.")
return
has_ball = input("Do you have a ball? (yes/no): ").strip().lower()
if age >= 5 and has_ball == "yes":
if age < 8:
group = "under 8 years old"
else:
group = "8 years old or older"
print(f"You can join the game! You are in the group: {group}.")
else:
print("Sorry, you cannot join the game.")
check_eligibility()
You can join the game! You are in the group: 8 years old or older.