3.6 Homework and Popcorn Hacks
3.6 Homework and Popcorn Hacks
Popcorn Hack 1
# Step 1: Add a variable that represents temperature
temperature = 65 # You can change this value to test different conditions
# Step 2: Check if it's a hot, warm, or cold day
if temperature >= 80:
print("It's a hot day")
elif 60 <= temperature < 80:
print("It's a warm day")
else:
print("It's a cold day")
## Verbal explanation of the code: Adding the elif statement adds an extra condition that is checked in the if-else statement.
## If the temperature is between 60 and 80, it will print "It's a warm day".
## If the temperature is less than 60, it will print "It's a cold day".
## The range also does not include 80 for the elif so it will go until 80 but not inclide 80.
It's a warm day
Popcorn Hack 2
%%js
let score = 85;
if (score >= 60) {
console.log("You passed!");
} else {
console.log("You failed.");
}
// Verbal explanation of the code:
// If the score is greater than or equal to 60, the code will print "You passed!".
// Otherwise, it will print "You failed.". This uses an if-else statement to check the score and determine the output.
<IPython.core.display.Javascript object>
Homework Hacks in Python
Hack 1: Odd or Even Checker
def check_odd_even(num): ## Function to check if a number is odd or even, the function takes a number as an argument
if num % 2 == 0: ## If the number is divisible by 2, it is even
print("Even")
else:
print("Odd")
check_odd_even(5)
check_odd_even(10)
check_odd_even(12)
Odd
Even
Even
Hack 2: Leap Year Checker
def is_leap_year(year):
# Check if the year is divisible by 4 but not by 100, or divisible by 400
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return "Leap Year"
else:
return "Not a Leap Year"
# Test the function with different years
print(is_leap_year(1804))
print(is_leap_year(1900))
print(is_leap_year(2074))
print(is_leap_year(2024))
Leap Year
Not a Leap Year
Not a Leap Year
Leap Year
Hack 3: Temperature Range Checker
def temperature_range(temperature): ## Function to check the temperature range, the function takes a temperature as an argument
if temperature >= 85: ## If the temperature is greater than or equal to 85, it is hot
return "Hot"
elif 60 <= temperature <= 80: ## If the temperature is between 60 and 80, it is warm
return "Warm"
else:
return "Cold" ## If the temperature is less than 60, it is cold
# Test the function with different temperatures
print(temperature_range(90))
print(temperature_range(70))
print(temperature_range(50))
Hot
Warm
Cold
Homework Hacks in Python
Hack 1: Check Voting Eligibility
%%js
function checkVotingEleigibility(age) {
if (age >= 18) {
return "You are eligible to vote!";
} else {
return "You are not eligible to vote yet.";
}
}
console.log(checkVotingEleigibility(20));
console.log(checkVotingEleigibility(15));
<IPython.core.display.Javascript object>
Hack 2: Grade Calculator
%%js
function getGrade(score) {
// if score is greater than or equal to 90, return "A"
if (score >= 90) {
return "A";
// if score is greater than or equal to 80, return "B"
} else if (score >= 80) {
return "B";
// if score is greater than or equal to 70, return "C"
} else if (score >= 70) {
return "C";
// if score is greater than or equal to 60, return "D"
} else if (score >= 60) {
return "D";
// if score is less than 60, return "F"
} else {
return "F";
}
}
console.log(getGrade(85));
console.log(getGrade(72));
console.log(getGrade(50));
console.log(getGrade(95));
Hack 3: Temperature Converter
%%js
function convertTemperature(value, scale) {
if (scale === "C") {
return (value * 9/5) + 32 + "°F";
} else if (scale === "F") {
return (value - 32) * 5/9 + "°C";
} else {
return "Invalid scale";
}
}
console.log(convertTemperature(32, "C"));
console.log(convertTemperature(100, "F"));
console.log(convertTemperature(0, "C"));
console.log(convertTemperature(212, "F"));
console.log(convertTemperature(50, "K"));
<IPython.core.display.Javascript object>