Rhea Rajashekhar P4 Popcorn Hacks 3.6
# Python homework hack 1
def check_odd_even(number):
# Check if the number is divisible by 2
if number % 2 == 0:
return "Even"
else:
return "Odd"
# Test 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(15)) # Output: Odd
print(check_odd_even(22)) # Output: Even
# python homework 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(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
# Python homework hack 3:
def temperature_range(temperature):
# Use if...elif...else to categorize the temperature
if temperature < 60:
return "Cold"
elif 60 <= temperature <= 80:
return "Warm"
elif temperature > 85:
return "Hot"
else:
return "Neither hot nor cold" # Handles temperatures between 81°F and 85°F
# Test the function with different temperature values
print(temperature_range(50)) # Output: Cold
print(temperature_range(70)) # Output: Warm
print(temperature_range(90)) # Output: Hot
print(temperature_range(83)) # Output: Neither hot nor cold
%% js
function checkVotingEligibility(age) {
// Use an if statement to check if the age is 18 or older
if (age >= 18) {
return "You are eligible to vote!";
} else {
return "You are not eligible to vote yet.";
}
}
// Test the function with different age values
console.log(checkVotingEligibility(20)); // Output: You are eligible to vote!
console.log(checkVotingEligibility(16)); // Output: You are not eligible to vote yet.
function getGrade(score) {
// Use if...else if...else statements to determine the letter grade
if (score >= 90) {
return "Grade: A";
} else if (score >= 80) {
return "Grade: B";
} else if (score >= 70) {
return "Grade: C";
} else {
return "Grade: F";
}
}
// Test the function with different scores
console.log(getGrade(95)); // Output: Grade: A
console.log(getGrade(85)); // Output: Grade: B
console.log(getGrade(75)); // Output: Grade: C
console.log(getGrade(65)); // Output: Grade: F
%% js
function convertTemperature(value, scale) {
// Use if statements to check the scale
if (scale === "C") {
// Convert Celsius to Fahrenheit
let fahrenheit = (value * 9/5) + 32;
return fahrenheit + "°F";
} else if (scale === "F") {
// Convert Fahrenheit to Celsius
let celsius = (value - 32) * 5/9;
return celsius.toFixed(2) + "°C"; // Round to 2 decimal places
} else {
return "Error: Invalid scale. Use 'C' for Celsius or 'F' for Fahrenheit.";
}
}
// Test the function with different values and scales
console.log(convertTemperature(25, "C")); // Output: 77°F
console.log(convertTemperature(77, "F")); // Output: 25.00°C
console.log(convertTemperature(100, "C")); // Output: 212°F
console.log(convertTemperature(32, "F")); // Output: 0.00°C
console.log(convertTemperature(30, "X")); // Output: Error: Invalid scale. Use 'C' or 'F'.