Skip to the content.

Homeworks 3.6

## Popcorn Hack 1 Python 3.6.1
# What would happen if you added more temperature ranges (like ‘warm’ for temperatures between 60 and 79 degrees)? How would you modify the code to implement this feature?”



def categorize_temperature(temp):
    if temp < 32:
        return "cold"
    elif 32 <= temp <= 59:
        return "cool"
    elif 60 <= temp <= 79:
        return "warm"
    elif temp >= 80:
        return "hot"

temperatures = [25, 45, 65, 85]
for temp in temperatures:
    category = categorize_temperature(temp)
    print(f"The temperature {temp}°F is categorized as: {category}")

The temperature 25°F is categorized as: cold
The temperature 45°F is categorized as: cool
The temperature 65°F is categorized as: warm
The temperature 85°F is categorized as: hot

Javascript Popcorn Hack 1 3.6.2

How would you change the code to show a message for scores below 60? What would you add?

%%js
function categorizeScore(score) {
    if (score >= 80) {
        return "Excellent";
    } else if (score >= 60) {
        return "Good";
    } else {
        return "Score is below 60. Needs Improvement";
    }
}
// Test cases
console.log(categorizeScore(85));  // Output: Excellent
console.log(categorizeScore(65));  // Output: Good
console.log(categorizeScore(55));  // Output: Score is below 60. Needs Improvement
<IPython.core.display.Javascript object>
## 3.6.4 Conditionals in Python Hack 1

# Define the function to check if a number is odd or even
def check_odd_even(number):
    if number % 2 == 0:
        return "Even"
    else:
        return "Odd"

# Test the function with different numbers
print(check_odd_even(10))  # Output: Even
print(check_odd_even(7))   # Output: Odd
print(check_odd_even(22))  # Output: Even
print(check_odd_even(15))  # Output: Odd

Even
Odd
Even
Odd
## 3.6.4 Conditionals in Python Hack 2

# Define the function to check if a year is a leap year
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"

# 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(2021))  # Output: Not a Leap Year
Leap Year
Not a Leap Year
Leap Year
Not a Leap Year
## 3.6.4 Conditionals in Python Hack 3

# Define the function to check the temperature range
def temperature_range(temperature):
    if temperature < 60:
        return "Cold"
    elif 60 <= temperature <= 80:
        return "Warm"
    elif temperature > 80:
        return "Hot"
    else:
        return "Undefined Range"  # For any edge cases, though unlikely

# Test the function with different temperatures
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

Unit 3.6.4 Conditionals in JavaScript Hack 1

%%js
public class Main {

    // Function to check voting eligibility
    public static String checkVotingEligibility(int age) {
        return (age >= 18) ? "You are eligible to vote!" : "You are not eligible to vote yet.";
    }

    // Main method to test the function
    public static void main(String[] args) {
        System.out.println(checkVotingEligibility(20));  // Output: You are eligible to vote!
        System.out.println(checkVotingEligibility(16));  // Output: You are not eligible to vote yet.
    }
}
<IPython.core.display.Javascript object>

Unit 3.6.4 Conditionals in JavaScript Hack 2

%%js
public class Main {

    // Function to determine letter grade based on the score
    public static String getGrade(int 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";
        }
    }

    // Main method to test the function
    public static void main(String[] args) {
        System.out.println(getGrade(95));  // Output: Grade: A
        System.out.println(getGrade(85));  // Output: Grade: B
        System.out.println(getGrade(75));  // Output: Grade: C
        System.out.println(getGrade(65));  // Output: Grade: F
    }
}
<IPython.core.display.Javascript object>

Unit 3.6.4 Conditionals in JavaScript Hack 3

%%js
public class Main {

    // Function to convert temperature between Celsius and Fahrenheit
    public static String convertTemperature(double value, String scale) {
        if (scale.equalsIgnoreCase("C")) {
            // Convert Celsius to Fahrenheit
            double fahrenheit = (value * 9/5) + 32;
            return value + "°C is " + fahrenheit + "°F";
        } else if (scale.equalsIgnoreCase("F")) {
            // Convert Fahrenheit to Celsius
            double celsius = (value - 32) * 5/9;
            return value + "°F is " + celsius + "°C";
        } else {
            return "Error: Invalid scale. Use 'C' for Celsius or 'F' for Fahrenheit.";
        }
    }

    // Main method to test the function
    public static void main(String[] args) {
        System.out.println(convertTemperature(25, "C"));  // Output: 25°C is 77.0°F
        System.out.println(convertTemperature(77, "F"));  // Output: 77°F is 25.0°C
        System.out.println(convertTemperature(100, "X")); // Output: Error: Invalid scale...
    }
}

<IPython.core.display.Javascript object>