![]() |
Home | Accounts | Setup | Verify | Play | Hacks |
3.7 Nested Conditinals all type of hacks
All hacks for Nested Conditionals
Popcorn Hack #1
Prompt the user for their age and whether they like sweets, then based on their age and preference, print either “You can have candy,” “You can have a healthy snack,” or “You get a fruit snack.”
# Python Version
age = int(input("Enter your age: "))
likes_small_pets = input("Do you prefer small pets? (yes/no): ").lower()
if age >= 16:
if likes_small_pets == "yes":
print("You might enjoy having a rabbit!")
else:
print("A dog could be a great companion for you.")
else:
print("A fish could be a perfect pet!")
A fish could be a perfect pet!
Popcorn Hack #2
Create a Python program that checks your savings against the prices of different laptops and informs you which one you can afford to buy.
# Prompt the user to enter their savings amount
savings = float(input("Enter your total savings: "))
# Define a dictionary with laptop models and their prices
laptops = {
"Basic Laptop": 500,
"Gaming Laptop": 1200,
"Ultrabook": 1000,
"Workstation": 1500,
"High-End Gaming Laptop": 2000
}
# Check which laptops the user can afford
affordable_laptops = [model for model, price in laptops.items() if savings >= price]
# Display results
if affordable_laptops:
print("With your savings, you can afford the following laptops:")
for laptop in affordable_laptops:
print(f"- {laptop}")
else:
print("Sorry, you don't have enough savings for any of the listed laptops.")
Sorry, you don't have enough savings for any of the listed laptops.
%%js
// Prompt the user to enter their savings amount
const savings = parseFloat(prompt("Enter your total savings: "));
// Define an object with laptop models and their prices
const laptops = {
"Basic Laptop": 500,
"Gaming Laptop": 1200,
"Ultrabook": 1000,
"Workstation": 1500,
"High-End Gaming Laptop": 2000
};
// Check which laptops the user can afford
let affordableLaptops = [];
for (const [model, price] of Object.entries(laptops)) {
if (savings >= price) {
affordableLaptops.push(model);
}
}
// Display results
if (affordableLaptops.length > 0) {
console.log("With your savings, you can afford the following laptops:");
affordableLaptops.forEach(laptop => console.log(`- ${laptop}`));
} else {
console.log("Sorry, you don't have enough savings for any of the listed laptops.");
}
<IPython.core.display.Javascript object>
Popcorn Hack #3
Create a simple JavaScript program that on the topic of your choice using boolean values and nested conditionals
%%js
// Prompt the user for their age and whether they have a driving license
const age = parseInt(prompt("Enter your age: "));
const hasLicense = confirm("Do you have a driving license?");
// Check if the person is eligible to drive
if (age >= 18) {
if (hasLicense) {
console.log("You are eligible to drive!");
} else {
console.log("You are old enough to drive, but you need to get a license first.");
}
} else {
console.log("You are not old enough to drive.");
}
<IPython.core.display.Javascript object>
Homework Hack #1
def get_user_input(prompt):
return input(prompt)
# Step 1: Get Age
age = int(get_user_input("Enter your age (e.g., 6, 10, etc.): "))
# Step 2: Check Equipment Ownership
has_equipment = get_user_input("Do you have a water bottle? (yes/no): ").lower()
# Step 3: Check Eligibility
if age >= 6 and has_equipment == "yes":
# Step 4: Determine Group
if age < 9:
group = "beginner group"
else:
group = "advanced group"
# Step 5: Display Result
print(f"You can join the activity in the '{group}'.")
else:
print("Sorry, you are not eligible to join. Make sure you are at least 6 years old and have a water bottle.")