3.6 HACKS
Student led teaching on Abstraction. Teaching how various data types can use abstraction for computational efficiency.
Popcorn Hack 1:
def suggest_outfit(temperature):
if temperature < 32:
return "It's freezing! Wear a heavy coat and a scarf."
elif 32 <= temperature < 60:
return "It's chilly! A sweater and jeans would be good."
elif 60 <= temperature < 80:
return "It's warm! A light jacket and comfortable pants would be great."
elif 80 <= temperature < 90:
return "It's a nice day! Wear a t-shirt and shorts."
else:
return "It's hot! Wear shorts and a tank top."
# Get user input
temp = float(input("Enter the temperature in degrees: "))
# Print the outfit suggestion
print(suggest_outfit(temp))
It's freezing! Wear a heavy coat and a scarf.
Popcorn Hack 2:
def password_checker():
# Define the correct password
correct_password = "my_secure_password"
# Initialize a variable to track the number of attempts
attempts = 0
# Set a maximum number of attempts
max_attempts = 3
while attempts < max_attempts:
# Ask the user for a password
user_password = input("Enter your password: ")
# Check if the entered password is correct
if user_password == correct_password:
print("Login successful! Welcome!")
break # Exit the loop on successful login
else:
attempts += 1 # Increment the attempt counter
print("Incorrect password. Try again.")
if attempts == max_attempts:
print("Maximum attempts reached. Access denied.")
# Call the password_checker function
password_checker()
Incorrect password. Try again.
Incorrect password. Try again.
Incorrect password. Try again.
Maximum attempts reached. Access denied.
Popcorn Hack 3:
def check_ages(person1_age, person2_age):
# Check if either person is a minor
if person1_age < 18:
print("Person 1 is a minor.")
else:
print("Person 1 is an adult.")
if person2_age < 18:
print("Person 2 is a minor.")
else:
print("Person 2 is an adult.")
# Example usage
age_person1 = int(input("Enter the age of Person 1: "))
age_person2 = int(input("Enter the age of Person 2: "))
check_ages(age_person1, age_person2)
Person 1 is a minor.
Person 2 is a minor.
Popcorn Hack 1:
function checkScore(score) {
// Check if the score is below 60
if (score < 60) {
console.log("Your score is below 60. You may need to improve your performance.");
} else if (score >= 60 && score < 70) {
console.log("You scored between 60 and 69. Keep it up!");
} else if (score >= 70 && score < 80) {
console.log("Great job! You scored between 70 and 79.");
} else if (score >= 80 && score < 90) {
console.log("Excellent! You scored between 80 and 89.");
} else {
console.log("Outstanding! You scored 90 or above!");
}
}
// Example usage
const userScore = prompt("Enter your score:");
checkScore(parseInt(userScore)); // Ensure the score is treated as a number
Popcorn Hack 2:
function checkScore(score) {
// Check if the score is exactly 50
if (score === 50) {
console.log("Your score is exactly 50. You might want to review the material.");
} else if (score < 50) {
console.log("Your score is below 50. You may need to improve your performance.");
} else if (score >= 60 && score < 70) {
console.log("You scored between 60 and 69. Keep it up!");
} else if (score >= 70 && score < 80) {
console.log("Great job! You scored between 70 and 79.");
} else if (score >= 80 && score < 90) {
console.log("Excellent! You scored between 80 and 89.");
} else {
console.log("Outstanding! You scored 90 or above!");
}
}
// Example usage
const userScore = prompt("Enter your score:");
checkScore(parseInt(userScore)); // Ensure the score is treated as a number
Popcorn Hack 3:
function checkScore(score) {
// Check if the score is exactly 50
if (score === 50) {
console.log("Your score is exactly 50. You might want to review the material.");
}
// Check if the score is between 60 and 69
else if (score >= 60 && score < 70) {
console.log("You scored between 60 and 69. Keep working hard to improve!");
}
// Check if the score is below 50
else if (score < 50) {
console.log("Your score is below 50. You may need to improve your performance.");
}
// Check if the score is between 70 and 79
else if (score >= 70 && score < 80) {
console.log("Great job! You scored between 70 and 79.");
}
// Check if the score is between 80 and 89
else if (score >= 80 && score < 90) {
console.log("Excellent! You scored between 80 and 89.");
}
// Check if the score is 90 or above
else {
console.log("Outstanding! You scored 90 or above!");
}
}
// Example usage
const userScore = prompt("Enter your score:");
checkScore(parseInt(userScore)); // Ensure the score is treated as a number
Hack 1:
def check_odd_even(number):
"""Check if a number is odd or even."""
if number % 2 == 0:
return "Even"
else:
return "Odd"
# Testing the function with different numbers
print(check_odd_even(4)) # Output: Even
print(check_odd_even(7)) # Output: Odd
print(check_odd_even(0)) # Output: Even
print(check_odd_even(-3)) # Output: Odd
Even
Odd
Even
Odd
Hack 2:
def is_leap_year(year):
"""Determine if a year is a leap year."""
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return "Leap Year"
else:
return "Not a Leap Year"
# Testing the function with different years
print(is_leap_year(2020)) # Output: Leap Year
print(is_leap_year(1900)) # Output: Not a Leap Year
print(is_leap_year(2000)) # Output: Leap Year
print(is_leap_year(2023)) # Output: Not a Leap Year
Leap Year
Not a Leap Year
Leap Year
Not a Leap Year
Hack 3:
def temperature_range(temperature):
"""Check the temperature range."""
if temperature < 60:
return "Cold"
elif 60 <= temperature <= 80:
return "Warm"
else: # temperature > 80
return "Hot"
# Testing the function with different temperature values
print(temperature_range(50)) # Output: Cold
print(temperature_range(75)) # Output: Warm
print(temperature_range(85)) # Output: Hot
print(temperature_range(90)) # Output: Hot
Cold
Warm
Hot
Hot