3.7 HACKS
Student led teaching on Abstraction. Teaching how various data types can use abstraction for computational efficiency.
python pop 1;
weather = "sunny"
transportation = "available"
boots = "present"
location_determined = True
miles_in_the_hike = 5
print(f"The weather is {weather}")
print(f"Your transportation is {transportation}")
print(f"Your boots are {boots}")
print(f"Location determined: {location_determined}")
print(f"Total miles in the hike: {miles_in_the_hike}")
if weather == "sunny":
if transportation == "available":
if boots == "present":
if location_determined and miles_in_the_hike <= 10:
print("You are ready to go hiking!")
else:
print("Check the location or make sure the hike isn't too long.")
else:
print("You need to find your boots first.")
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 present
Location determined: True
Total miles in the hike: 5
You are ready to go hiking!
python pop 2;
living_room_available = True
projector_working = False
enough_snacks = True
if living_room_available:
if projector_working:
if enough_snacks:
print("You're ready to host the movie night!")
else:
print("You need to get more snacks. Ideas: Popcorn, Candy, Soda")
else:
print("You need to fix or replace the projector.")
else:
print("The living room is not available for the movie night. Find another room!")
You need to fix or replace the projector.
python pop 3;
import random
# Random values for gas, weather, and car check
enough_gas = random.choice([True, False]) # Boolean: Do you have enough gas?
gas_in_gallons = random.randint(5, 20) # Random gas amount between 5 to 20 gallons
required_gallons = 10 # Assume you need 10 gallons for the trip
# Random values for weather and car
good_weather = random.choice([True, False]) # Random: Is the weather good?
car_checked = random.choice([True, False]) # Random: Did the car pass the check-up?
# Integer budget and required amount
budget = random.randint(50, 200) # Random budget between $50 and $200
required_budget = 100 # Assume you need $100 for the trip
# Nested conditionals to check everything
if enough_gas and gas_in_gallons >= required_gallons:
print(f"You have enough gas! You have {gas_in_gallons} gallons, which is sufficient.")
if good_weather:
print("The weather is good!")
if car_checked:
print("Your car has been checked!")
if budget >= required_budget:
print(f"You're ready for the road trip! You have ${budget}, which is enough.")
else:
print(f"You need more money for the trip. You only have ${budget}, but you need at least ${required_budget}.")
else:
print("You need to get the car checked first.")
else:
print("The weather isn't good for a road trip.")
else:
if not enough_gas:
print("You need to fill up the tank first.")
else:
print(f"You don't have enough gas! You only have {gas_in_gallons} gallons, but you need at least {required_gallons}.")
You need to fill up the tank first.
Javascript pop 1;
let weather = "sunny";
let transportation = "available";
let boots = "present";
let location_determined = true;
let miles_in_the_hike = 5;
console.log("The weather is " + weather);
console.log("Your transportation is " + transportation);
console.log("Your boots are " + boots);
if (weather === "sunny") {
if (transportation === "available") {
if (boots === "present") {
if (location_determined && miles_in_the_hike <= 10) {
console.log("You are ready to go hiking!");
} else {
console.log("Check the location or make sure the hike isn't too long.");
}
} else {
console.log("You need to find your boots first.");
}
} else {
console.log("You need to arrange transportation.");
}
} else {
console.log("It's not good weather for hiking.");
}
javascript pop 2;
let living_room_available = true;
let projector_working = false;
let enough_snacks = true;
if (living_room_available) {
if (projector_working) {
if (enough_snacks) {
console.log("You're ready to host the movie night!");
} else {
console.log("You need to get more snacks. Ideas: Popcorn, Candy, Soda");
}
} else {
console.log("You need to fix or replace the projector.");
}
} else {
console.log("The living room is not available for the movie night. Find another room!");
}
javascript pop 3;
// Boolean: Do you have enough gas?
let enough_gas = Math.random() < 0.5; // Random true or false
let gas_in_gallons = Math.floor(Math.random() * (20 - 5 + 1)) + 5; // Random gas amount between 5 and 20 gallons
let required_gallons = 10; // Assume you need 10 gallons for the trip
// Random: Is the weather good? (Random true or false)
let good_weather = Math.random() < 0.5;
// Random: Did the car pass the check-up? (Random true or false)
let car_checked = Math.random() < 0.5;
// Integer: Your current budget (Random between $50 and $200)
let budget = Math.floor(Math.random() * (200 - 50 + 1)) + 50;
let required_budget = 100; // Assume you need $100 for the trip
// Check the conditions using nested conditionals
if (enough_gas && gas_in_gallons >= required_gallons) {
console.log(`You have enough gas! You have ${gas_in_gallons} gallons, which is sufficient.`);
if (good_weather) {
console.log("The weather is good!");
if (car_checked) {
console.log("Your car has been checked!");
if (budget >= required_budget) {
console.log(`You're ready for the road trip! You have $${budget}, which is enough.`);
} else {
console.log(`You need more money for the trip. You only have $${budget}, but you need at least $${required_budget}.`);
}
} else {
console.log("You need to get the car checked first.");
}
} else {
console.log("The weather isn't good for a road trip.");
}
} else {
if (!enough_gas) {
console.log("You need to fill up the tank first.");
} else {
console.log(`You don't have enough gas! You only have ${gas_in_gallons} gallons, but you need at least ${required_gallons}.`);
}
}
python hack 1;
# Hack 1: Go to the Beach Decision
weather = "sunny" # Change this to test different conditions
has_sunscreen = True # Change to False to simulate no sunscreen
has_snacks = False # Change to True to simulate having snacks
print(f"The weather is {weather}.")
print(f"Do you have sunscreen? {has_sunscreen}")
print(f"Do you have snacks? {has_snacks}")
if weather == "sunny":
if has_sunscreen:
if has_snacks:
print("You are ready for the beach!")
else:
print("You need to get some snacks before going to the beach.")
else:
print("You need to buy sunscreen before going to the beach.")
else:
print("It's not a good day for the beach.")
The weather is sunny.
Do you have sunscreen? True
Do you have snacks? False
You need to get some snacks before going to the beach.
python hack 2;
# Hack 2: Adopt a Pet
age = 20 # Change this to test different ages
space_in_home = 60 # Change this to test different space sizes
available_to_care = True # Change to False to simulate no availability
print(f"Your age is {age}.")
print(f"You have {space_in_home} square feet of space.")
print(f"Are you available to care for the pet? {available_to_care}")
if age >= 18:
if space_in_home > 50:
if available_to_care:
print("You can adopt the pet!")
else:
print("You need to make time to care for the pet.")
else:
print("You need a bigger home with more space to adopt a pet.")
else:
print("You must be at least 18 years old to adopt a pet.")
Your age is 20.
You have 60 square feet of space.
Are you available to care for the pet? True
You can adopt the pet!
python hack 3;
# Hack 3: Participate in a Marathon
weather = "clear" # Change this to test different weather conditions
has_running_shoes = True # Change to False to simulate no running shoes
days_practiced = 12 # Change to test different number of practice days
print(f"The weather is {weather}.")
print(f"Do you have running shoes? {has_running_shoes}")
print(f"How many days have you practiced? {days_practiced}")
if weather == "clear":
if has_running_shoes:
if days_practiced >= 10:
print("You are ready for the marathon!")
else:
print("You need to practice more before the marathon.")
else:
print("You need to buy running shoes before the marathon.")
else:
print("It's not the right time for the marathon due to the weather.")
The weather is clear.
Do you have running shoes? True
How many days have you practiced? 12
You are ready for the marathon!
javascript hack 1;
// Hack 1: Study for an Exam
let hasStudyMaterials = true; // Change to false to simulate no materials
let hasQuietPlace = false; // Change to true to simulate having a quiet place
let isTired = false; // Change to true to simulate being too tired
console.log("Do you have your study materials? " + hasStudyMaterials);
console.log("Do you have a quiet place to study? " + hasQuietPlace);
console.log("Are you feeling tired? " + isTired);
if (hasStudyMaterials) {
if (hasQuietPlace) {
if (!isTired) {
console.log("You are ready to study!");
} else {
console.log("You are too tired. Consider taking a nap or resting first.");
}
} else {
console.log("You need to find a quiet place to study.");
}
} else {
console.log("You need to gather your study materials first.");
}
javascript hack 2;
// Hack 2: Bake a Cake
let hasIngredients = true; // Change to false to simulate missing ingredients
let ovenWorking = true; // Change to false to simulate oven not working
let availableTime = 3; // Hours available (change to test)
let requiredTime = 2; // Time required to bake and cool the cake
console.log("Do you have all ingredients? " + hasIngredients);
console.log("Is the oven working? " + ovenWorking);
console.log("Available time: " + availableTime + " hours");
if (hasIngredients) {
if (ovenWorking) {
if (availableTime >= requiredTime) {
console.log("You can start baking the cake!");
} else {
console.log("You need to find more time to bake the cake.");
}
} else {
console.log("You need to fix or replace the oven before baking.");
}
} else {
console.log("You are missing some ingredients to bake the cake.");
}
javascript hack 3;
// Hack 3: Go Camping
let weather = "clear"; // Change to "rainy" or other values to test
let hasTent = true; // Change to false to simulate no tent
let hasFoodAndWater = true; // Change to false to simulate not enough food or water
console.log("The weather is " + weather);
console.log("Do you have a tent? " + hasTent);
console.log("Do you have enough food and water? " + hasFoodAndWater);
if (weather === "clear") {
if (hasTent) {
if (hasFoodAndWater) {
console.log("You are ready to go camping!");
} else {
console.log("You need to pack more food and water for the trip.");
}
} else {
console.log("You need to buy or borrow a tent.");
}
} else {
console.log("It's not a good time for camping due to the weather.");
}