# Step 1: Add a variable that represents temperature
temperature = 50 # You can change this value to test different conditions
if 65 <= temperature <= 80:
print("It's a comfortable day")
# Step 2: Check if it’s a hot day
else:
print("I don't like this weather.")
I don't like this weather.
Popcorn Hack #1
- Conditions in JavaScript
- How would you change the code to show a message for scores below 60? What would you add?
%%js
let score = 50;
if (50 >= 60) {
console.log("You passed!");
} else {
console.log("Maybe study!");
}
<IPython.core.display.Javascript object>
Python HW Hack #1
# Define the function check_odd_even
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(10)) # Output: Even
print(check_odd_even(7)) # Output: Odd
print(check_odd_even(0)) # Output: Even
print(check_odd_even(33)) # Output: Odd
Even
Odd
Even
Odd
Python HW Hack #2
# Define the function is_leap_year
def is_leap_year(year):
# Check if the year is divisible by 4 but not 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 HW Hack #3
# Define the function temperature_range
def temperature_range(temperature):
# Use if...elif...else statements to categorize the temperature
if temperature < 60:
return "Cold"
elif 60 <= temperature <= 80:
return "Warm"
elif temperature > 85:
return "Hot"
else:
return "Undefined Range" # For temperatures between 81°F and 85°F
# Test the function with different temperature values
print(temperature_range(55)) # Output: Cold
print(temperature_range(75)) # Output: Warm
print(temperature_range(90)) # Output: Hot
print(temperature_range(83)) # Output: Undefined Range
JavaScript HW Hack #1
%%js
public class VotingEligibility {
// Define the function checkVotingEligibility
public static String checkVotingEligibility(int age) {
// 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.";
}
}
public static void main(String[] args) {
// Test the function with different age values
System.out.println(checkVotingEligibility(20)); // Output: You are eligible to vote!
System.out.println(checkVotingEligibility(16)); // Output: You are not eligible to vote yet.
System.out.println(checkVotingEligibility(18)); // Output: You are eligible to vote!
}
}
<IPython.core.display.Javascript object>
JavaScript HW Hack #2
%%js
public class GradeCalculator {
// Define the function getGrade
public static String getGrade(int 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";
}
}
public static void main(String[] args) {
// Test the function with different scores
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>
JavaScript HW Hack #3
%%js
public class TemperatureConverter {
// Define the function convertTemperature
public static String convertTemperature(double value, String scale) {
// Check if the scale is "C" or "F"
if (scale.equalsIgnoreCase("C")) {
// Convert Celsius to Fahrenheit
double fahrenheit = (value * 9/5) + 32;
return String.format("%.2f°C is %.2f°F", value, fahrenheit);
} else if (scale.equalsIgnoreCase("F")) {
// Convert Fahrenheit to Celsius
double celsius = (value - 32) * 5/9;
return String.format("%.2f°F is %.2f°C", value, celsius);
} else {
// If scale is neither "C" nor "F", return an error message
return "Error: Invalid scale. Please use 'C' for Celsius or 'F' for Fahrenheit.";
}
}
public static void main(String[] args) {
// Test the function with different values and scales
System.out.println(convertTemperature(25, "C")); // Output: 25.00°C is 77.00°F
System.out.println(convertTemperature(77, "F")); // Output: 77.00°F is 25.00°C
System.out.println(convertTemperature(100, "X")); // Output: Error: Invalid scale. Please use 'C' or 'F'.
}
}
<IPython.core.display.Javascript object>