Homework Hacks 3.6
Homework Hacks 3.6
# Popcorn Hack #1
# Step 1: Add a variable that represents temperature
temperature = 60 # You can change this value to test different conditions
# Step 2: Check if it’s a hot day
if temperature >= 80:
print("It's a hot day")
# Step 3: Check if it’s a warm day
elif 60 <= temperature < 80:
print("It's a warm day")
# Step 4: Check if it’s a cool day
elif 40 <= temperature < 60:
print("It's a cool day")
# Step 5: Otherwise, print it's a cold day
else:
print("It's a cold day")
It's a warm day
3.6.2 Hacks
// Popcorn Hack #1
let score = 85;
if (score >= 60) {
console.log("You passed!");
} else {
console.log("You failed! Your score is below 60.");
}
Cell In[7], line 1
// Popcorn Hack #1
^
SyntaxError: invalid syntax
3.6.3 Python Hacks
# Hack 1: Odd or Even Checker
def check_odd_even(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"
def get_valid_number():
while True:
user_input = input("Enter a number to check if it's odd or even: ")
if user_input.isdigit() or (user_input.startswith('-') and user_input[1:].isdigit()):
return int(user_input)
else:
print("Invalid input. Please enter a valid number.")
while True:
number = get_valid_number()
result = check_odd_even(number)
print(f"The number {number} is {result}.")
continue_check = input("Do you want to check another number? (yes/no): ").strip().lower()
if continue_check != 'yes':
break
print(check_odd_even(4))
print(check_odd_even(7))
print(check_odd_even(10))
print(check_odd_even(15))
print(check_odd_even(0))
The number 1 is Odd.
Even
Odd
Even
Odd
Even
# Hack 2: Leap Year Checker
def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return "Leap Year"
else:
return "Not a Leap Year"
def get_valid_year():
while True:
user_input = input("Enter a year to check if it's a leap year: ")
if user_input.isdigit() or (user_input.startswith('-') and user_input[1:].isdigit()):
return int(user_input)
else:
print("Invalid input. Please enter a valid year.")
while True:
year = get_valid_year()
result = is_leap_year(year)
print(f"The year {year} is {result}.")
continue_check = input("Do you want to check another year? (yes/no): ").strip().lower()
if continue_check != 'yes':
break
print(is_leap_year(2000))
print(is_leap_year(1900))
print(is_leap_year(2024))
print(is_leap_year(2023))
print(is_leap_year(1600))
The year 1999 is Not a Leap Year.
Leap Year
Not a Leap Year
Leap Year
Not a Leap Year
Leap Year
#Hack 3: Temperature Range Checker
def temperature_range(temperature):
if temperature < 60:
return "Cold"
elif 60 <= temperature <= 80:
return "Warm"
elif temperature > 85:
return "Hot"
else:
return "Moderate"
def get_valid_temperature():
while True:
user_input = input("Enter a temperature to check its range: ")
if user_input.replace('.', '', 1).isdigit() or (user_input.startswith('-') and user_input[1:].replace('.', '', 1).isdigit()):
return float(user_input)
else:
print("Invalid input. Please enter a valid number.")
while True:
temperature = get_valid_temperature()
result = temperature_range(temperature)
print(f"The temperature {temperature}°F is considered {result}.")
continue_check = input("Do you want to check another temperature? (yes/no): ").strip().lower()
if continue_check != 'yes':
break
print(temperature_range(55))
print(temperature_range(65))
print(temperature_range(75))
print(temperature_range(85))
print(temperature_range(90))
The temperature 100.0°F is considered Hot.
The temperature 30.0°F is considered Cold.
The temperature 60.0°F is considered Warm.
Cold
Warm
Warm
Moderate
Hot
3.6.4 Javascript Hacks
// Hack #2: Checking Voting Eligibility
function checkVotingEligibility(age) {
if (age >= 18) {
return "You are eligible to vote!";
} else {
return "You are not eligible to vote yet.";
}
}
function getValidAge() {
while (true) {
let userInput = prompt("Enter your age to check voting eligibility: ");
if (!isNaN(userInput) && userInput.trim() !== "") {
return parseInt(userInput, 10);
} else {
alert("Invalid input. Please enter a valid age.");
}
}
}
while (true) {
let age = getValidAge();
let result = checkVotingEligibility(age);
console.log(result);
let continueCheck = prompt("Do you want to check another age? (yes/no): ").trim().toLowerCase();
if (continueCheck !== 'yes') {
break;
}
}
// Hack 2: Grade Calculator
function getGrade(score) {
if (score >= 90) {
return "Grade: A";
} else if (score >= 80) {
return "Grade: B";
} else if (score >= 70) {
return "Grade: C";
} else {
return "Grade: F";
}
}
console.log(getGrade(95));
console.log(getGrade(85));
console.log(getGrade(75));
console.log(getGrade(65));
console.log(getGrade(50));
Cell In[6], line 1
// Hack 2: Grade Calculator
^
SyntaxError: invalid syntax
// Hack 3: Temperature Converter
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 "Error: Invalid scale. Use 'C' for Celsius or 'F' for Fahrenheit.";
}
}
console.log(convertTemperature(0, "C"));
console.log(convertTemperature(32, "F"));
console.log(convertTemperature(100, "C"));
console.log(convertTemperature(212, "F"));
console.log(convertTemperature(50, "X"));