conditionals
#3.6.1 python popcorn hack 1
temperature = 75 # initial variable at 50
#hot day if greater than equal to 80
if temperature >= 80:
print("It's a hot day")
#addition to original code: else if --> check for warm day if greater than equal to 60 degrees
elif temperature >= 60:
print("it's a warm day")
#if if condition not met, it is a cold day
else:
print("It's a cold day")
it's a warm day
%% js
//3.6.2 js popcorn hack
let score = 55; //testing to see if 85 is a passing score
if (score >= 60) { //if score greater than equal 60, print "you passed"
console.log("You passed!");
}
else { //if above condition not met, then score is less than 60 and print failed message
console.log("you failed dummy go study");
}
3.6.3 python homework hacks:
#hack 1: odd or even checker
def check_odd_number(number):
if number % 2 == 0: #checks if remainder after divinding by 2 is 0 or not
return "even" #if no remainder, is divisibile by 2 and therefore even
else:
return "odd" #if there is remainder, is odd
#testing with different numbers
print(check_odd_number(12))
print(check_odd_number(35))
print(check_odd_number(8))
print(check_odd_number(92))
print(check_odd_number(47))
even
odd
even
even
odd
#hack 2: leap year checker
def is_leap_year(year):
#if divisibile by 4 and not by 100 or divisible by 400, return is "leap year"
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return "leap year"
else:
return "not a leap year"
print(is_leap_year(1963))
print(is_leap_year(2020))
print(is_leap_year(2012))
print(is_leap_year(1800))
print(is_leap_year(1849))
not a leap year
leap year
leap year
not a leap year
not a leap year
#hack 3: tempearture range checker
def temperature_range(temperature):
if temperature < 60:
return "cold"
elif 60 <= temperature <= 80:
return "warm"
else:
return "hot"
print(temperature_range(55))
print(temperature_range(70))
print(temperature_range(90))
cold
warm
hot
3.6.4 js homework hack
// hack 1: check voting eligibility
function checkVotingEligibility(age){
if (age >= 18){ //check if age greaterthan equal to 18
return "you are eligible to vote!";
}
else{
return "you are not eligible to vote yet";
}
}
console.log(checkVotingEligibility(16));
console.log(checkVotingEligibility(18));
console.log(checkVotingEligibility(54));
//hack 2: grade calculator
function getGrade(score){
if (score >= 90){ //score greaterthan equal to 90, get A
return "Grade: A";
}
else if ( score >= 80 ){ //score between 80-89, get B
return "Grade: B";
}
else if ( score >= 70 ){ //score between 70-79, get C
return "Grade: C";
}
else{ //score under 69 get F
return "Grade: F";
}
}
console.log(getGrade(95));
console.log(getGrade(85));
console.log(getGrade(75));
console.log(getGrade(55));
//hack 3: temperature converter
function convertTemperature(value, scale){
if (scale === "C"){ //check for celsius, return with Fahrenheit conversion
return (value * 9/5) +32 + "°F";
}
else if (scale === "F"){ //check for Fahrenheit, return with celcius conversion
return (value -32) * 5/9 + "°C";
}
else{
return "error, neither fahrenheit nor celsius";
}
}
console.log(convertTemperature(85, "F"));
console.log(convertTemperature(32, "C"));
console.log(convertTemperature(78, "N"));
3.7.1: nested conditionals
#3.7.1 python popcorn hack
weather = "sunny"
transportation = "available"
boots = "not present"
location = "far away"
print("The weather is " + weather)
print("Your transportation is " + transportation)
print("Your boots are " + boots)
if weather == "sunny": #checking good weather, since it is move onto next condition
if transportation == "available": #check transportation, since available, move to boots
if boots == "present": #boots not present, print else statement
print("You are ready to go hiking!")
else:
print("You need to find your boots first.")
# my own conditional (location)
if location == "not far away": #check location distance, since it is far away print else statement
print("you are ready to go")
else:
print("you should prepare more")
else:
print("You need to arrange transportation.")
else:
print("It's not good weather for hiking.")
The weather is sunny
Your transportation is available
Your boots are not present
You need to find your boots first.
you should prepare more
// 3.7.2 js popcorn hack
let weather = "sunny";
let transportation = "available";
let boots = "not present";
let distance = "very far away";
console.log("The weather is " + weather);
console.log("Your transportation is " + transportation);
console.log("Your boots are " + boots);
console.log("The distance is " + distance);
if (weather === "sunny") { //see python for explination comments, they are same just different syntax for js
if (transportation === "available") {
if (boots === "present") {
console.log("You are ready to go hiking!");
} else {
console.log("You need to find your boots first.");
}
//the condition I added: distance
if (distance === "not very far away") {
console.log("you are ready for hiking");
} else {
console.log ("you need to prepare more because it's far away")
}
} else {
console.log("You need to arrange transportation.");
}
} else {
console.log("It's not good weather for hiking.");
}
3.7.3 and homework hacks
#hack 1:Write Python pseudocode to decide whether or not to go to the beach
BEGIN
// Step 1: Define variables for conditions
SET weather = "sunny" // Example values: "sunny", "cloudy", "rainy"
SET has_sunscreen = TRUE // Example values: TRUE or FALSE
SET enough_snacks = TRUE // Example values: TRUE or FALSE
// Step 2: Check conditions for going to the beach
IF weather IS NOT "sunny" THEN
PRINT "It's not a good day for the beach."
ELSE
IF NOT has_sunscreen THEN
PRINT "You should buy sunscreen."
ELSE
IF NOT enough_snacks THEN
PRINT "You should get snacks first."
ELSE
PRINT "You are ready for the beach!"
END IF
END IF
END IF
END
#hack 2: Write a Python code that checks if you can adopt a pet
def adopt_pet_eligibility(age, space, availbility):
if age < 18: #check age, if younger than 18, print too young. If old enough, move to next condition
print("Your too young, wait until your 18")
elif space <= 50: #if house too small, print message, if big enough move to next condition
print ("your house is too small, focus on getting rich first")
elif not availbility: #if avalibale, can adopt, if not print message
print("you need to manage your time better to properly take care of the pet")
else:
print("You are eligible to adopt the pet!")
adopt_pet_eligibility(15, 50, True)
adopt_pet_eligibility(20, 10, True)
adopt_pet_eligibility(28, 70, False)
adopt_pet_eligibility(32, 70, True)
Your too young, wait until your 18
your house is too small, focus on getting rich first
you need to manage your time better to properly take care of the pet
You are eligible to adopt the pet!
#hack 3: Write Python code to determine whether or not to participate in a marathon.
def marathon_participation (weather, shoes, practice):
if weather.lower() != "clear":
print("not right time for the marathon")
elif not shoes:
print("you need to buy shoes first")
elif practice < 10:
print("you need to practice more")
else:
print("you are ready for the marathon!")
marathon_participation ("rainy", True, 11)
marathon_participation ("clear", False, 11)
marathon_participation ("clear", True, 8)
marathon_participation ("clear", True, 12)
not right time for the marathon
you need to buy shoes first
you need to practice more
you are ready for the marathon!
3.7.4 js homework hacks
//hack 1: study for exam?
// Function to decide whether to study for an exam
function decideToStudy(studyMaterials, quietPlace, feelingTired) {
// Check if study materials are available
if (!studyMaterials) {
print("You need to gather your study materials first.");
}
// Check if there is a quiet place to study
else if (!quietPlace) {
print("Find a quiet location to study.");
}
// Check if you are feeling too tired
else if (feelingTired) {
print("You should take a nap or rest first.");
}
// All conditions are met
else {
print("You are ready to study!");
}
}
// Example usage
decideToStudy(true, true, false); // Output: You are ready to study!
decideToStudy(false, true, false); // Output: You need to gather your study materials first.
decideToStudy(true, false, false); // Output: Find a quiet location to study.
decideToStudy(true, true, true); // Output: You should take a nap or rest first.
//hack 2: bake a cake?
function canBakeCake(ingredients, ovenWorking, availableTime) {
let missingIngredients = [];
if (!ingredients.flour) { //check if have flour
missingIngredients.push("flour");
}
if (!ingredients.eggs) { //check if have eggs
missingIngredients.push("eggs");
}
if (!ingredients.sugar) { //check if have sugar
missingIngredients.push("sugar");
}
if (missingIngredients.length > 0) { //if missing ingredient great than 0 (meaning an ingredient is missing), print missing ingredient
console.log("You are missing: " + missingIngredients.join(", ") + ".");
}
else if (!ovenWorking) { //check oven working
console.log("You need to fix or replace the oven.");
}
else if (availableTime < 2) { //check avalibility
console.log("You need to find more time.");
}
else { //if all conditions are met print ready to bake message
console.log("You can start baking!");
}
}
//example set of ingrediences:
let ingredients = {
flour: true,
eggs: true,
sugar: true
};
canBakeCake(ingredients, true, 2);
canBakeCake(ingredients, false, 2);
canBakeCake(ingredients, true, 1);
canBakeCake({flour: true, eggs: false, sugar: true}, true, 2);
//hack 3: camping trip?
function canGoCamping(weather, tent, foodAndWater) {
if (!weather) { //check if weather is clear, if it is move to next condition, if not print message
console.log("It's a bad time for camping.");
}
else if (!tent) { //check if have tent, if so move to food/ water, if not print message
console.log("Go buy or borrow a tent.");
}
else if (!foodAndWater) { //check food and water, if so all conditions are met and can go camping, if not, print message
console.log("You need to pack more food and water.");
}
else {
console.log("You're ready for a camping trip");
}
}
let weatherIsClear = true;
let tent = true;
let hasEnoughFoodAndWater = true;
canGoCamping(weatherIsClear, tent, hasEnoughFoodAndWater);
canGoCamping(false, true, true);
canGoCamping(true, false, true);
canGoCamping(true, true, false);