3.6 and 3.7 Popcorn and Homework Hacks
Yash's Submission for 3.6 and 3.7 Homework
Conditionals
Popcorn Hack 1
(Python and Javascript)
num1 = input("Enter a number: ")
if len(num1) == 1:
print("single digits")
elif len(num1) == 2:
print("double digits")
elif len(num1) == 3:
print("triple digits")
else:
print("I don't know what you are talking about")
double digits
%%js
function digits(num) {
if (num >= 1 && num <= 9) {
return "single digits";
} else if (num >= 10 && num <= 99) {
return "double digits";
} else if (num >= 100 && num <= 999) {
return "triple digits";
} else {
return "invalid number";
}
}
console.log(digits(5));
console.log(digits(53));
console.log(digits(501));
<IPython.core.display.Javascript object>
Popcorn Hack #2
(Only Python)
raining = True
if raining:
print("Take an umbrella")
else:
print("Don't take an umbrella")
Take an umbrella
Popcorn Hack #3
(Only Javascript)
%%js
let moment = require('moment');
//let option = input("Enter a date format: ");
// change to hardset variable
let option = "MM/DD/YYYY";
if (option === "MM/DD/YYYY") {
console.log(moment().format("MM/DD/YYYY"));
}
else if (option === "DD/MM/YYYY") {
console.log(moment().format("DD/MM/YYYY"));
}
else {
console.log("Invalid date format");
}
<IPython.core.display.Javascript object>
Homework Hack
correct = 0
q1 = input("1. How many points is a free throw worth? (a) 1 (b) 2 (c) 3: ")
if q1.lower() == "a":
correct += 1
q2 = input("2. How long is an NBA quarter? (a) 10 minutes (b) 12 minutes (c) 15 minutes: ")
if q2.lower() == "b":
correct += 1
q3 = input("3. How many players from each team are on the court during a basketball game? (a) 4 (b) 5 (c) 6: ")
if q3.lower() == "b":
correct += 1
q4 = input("4. Which NBA player is known for winning 6 championships with the Chicago Bulls? (a) Michael Jordan (b) Kobe Bryant (c) LeBron James: ")
if q4.lower() == "a":
correct += 1
q5 = input("5. Which team has won the most NBA championships? (a) Los Angeles Lakers (b) Boston Celtics (c) Golden State Warriors: ")
if q5.lower() == "b":
correct += 1
print(f"You scored {correct}/5!")
You scored 4/5!
Nested Conditionals
Popcorn Hack #1
(Python and Javascript)
age = int(input("Enter your age: "))
likes_sports = input("Do you like sports? (yes/no): ").lower()
if age >= 18:
if likes_sports == "yes":
print("You can join a local sports team!")
else:
print("You can try some indoor exercises.")
else:
print("You should focus on school sports activities!")
You can join a local sports team!
%%js
//let age = parseInt(prompt("Enter your age:"));
//let likesSports = prompt("Do you like sports? (yes/no):").toLowerCase();
// change to hardset variable
let age = 20;
let likesSports = "yes";
if (age >= 18) {
if (likesSports === "yes") {
console.log("You can join a local sports team!");
} else {
console.log("You can try some indoor exercises.");
}
} else {
console.log("You should focus on school sports activities!");
}
<IPython.core.display.Javascript object>
Popcorn Hack 2
budget = 300
ps5_price = 500
xbox_price = 400
nintendo_switch_price = 300
if budget >= ps5_price:
print("You can buy a PS5!")
elif budget >= xbox_price:
print("You can buy an Xbox!")
elif budget >= nintendo_switch_price:
print("You can buy a Nintendo Switch!")
else:
print("You don't have enough money to buy a console.")
You can buy a Nintendo Switch!
Popcorn Hack 3
%%js
let is_weather_good = true;
let are_friends_available = false;
if (is_weather_good) {
console.log("It's a great day to be outside!");
if (are_friends_available) {
console.log("You can organize a soccer game with friends.");
} else {
console.log("Go for a solo hike or bike ride.");
}
} else {
console.log("The weather isn't good, consider staying indoors.");
}
<IPython.core.display.Javascript object>
Homework Hack
I made this my own by changing it to a bike race with unique categories!
age = int(input("Enter your age: "))
has_bike = input("Do you have a bike? (yes/no): ").lower()
can_join_race = age >= 10 and has_bike == "yes"
if can_join_race:
if age < 15:
message = "You will race in the junior category."
else:
message = "You will race in the senior category."
print(f"Awesome! {message}")
else:
print("Sorry, you can't join the race yet.")
Awesome! You will race in the senior category.