Skip to the content.
Home Accounts Setup Verify Play Hacks

Gaheera Babbar P4 Popcorn Hacks 3.6

3.6 Lesson Popcorn and Homework Hacks

Python

#Python Conditional Popcorn Hack 1: Adding Temperature Ranges and Modyifying Code
#Adding more temperature ranges would require the addition of more if statements and result in more possible outputs.
# Step 1: Add a variable that represents temperature
temperature = 50  # 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")
elif temperature >= 65:
    print("It's a warm day")
# Step 3: Otherwise, print it's a cold day
else:
    print("It's a cold day")

It's a cold day
#Python Conditionals Bonus Popcorn Hack 1: How would you modify the code to include a message for a successful login attempt? What additional condition would you need to implement this?
#You would need a condition to test if the log in attempt was successful and include a statement sayong what the output statement should be if it was.
# Step 1: Create a variable called is_logged_in
is_logged_in = False  # You can change this to True to test the other condition

# Step 2: Create a variable to represent the success of the login attempt
login_success = True  # Change this to False to test unsuccessful login

# Step 3: Check if the user is logged in
if is_logged_in:
    print("Welcome back!")
# Step 4: Check if the login attempt was successful
elif login_success:
    print("Login successful! You are now logged in.")
else:
    # Step 5: Otherwise, prompt the user to log in
    print("Please log in.")

%%js
// JS Conditionals Popcorn Hack 1: Message for scores below 60
// I would add an else statement for scores that are not greater than or equal to 60.
let score = 85;

if (score >= 60) {
    console.log("You passed!");
} else {
    console.log("You failed. Better luck next time!");
}



%%js
// JS Conditionals Bonus Popcorn Hack 1: Message for score of exactly 50
// I would add an else if statement for scores that are not greater than 60 and are exactly 50.
let score = 50;

if (score > 60) {
    console.log("You passed!");
} else if (score === 50) {
    console.log("You scored exactly 50. Keep trying!");
} else {
    console.log("You failed!");
}

%%js
// JS Conditionals Bonus Popocorn Hack 2: Message for scores between 60 and 69
// I would add a condition and corresponding grade for scores between 60 to 69
if (score >= 90) {
    console.log("Grade: A");
} else if (score >= 80) {
    console.log("Grade: B");
} else if (score >= 70) {
    console.log("Grade: C");
} else if (score >= 60) {
    console.log("Grade: D");  // Added for scores between 60 and 69
} else {
    console.log("Grade: F");
}

<IPython.core.display.Javascript object>
#Python Homework Hack 1: Odd or Even Checker
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


Even
Odd
Even
Odd
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

Leap Year
Not a Leap Year
Leap Year
Not a Leap Year
#Python Homework Hack 3: Temperature Range Checker
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

Cold
Warm
Hot
Neither hot nor cold
%%js
// JS Homework Hack 1: Check Voting Age Eligibility
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.
<IPython.core.display.Javascript object>
%% js
// JS Homework Hack 2: Grade Calculator
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
// JS Homework Hack 3: Temperature Converter
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' for Celsius or 'F' for Fahrenheit.