# Nested conditionals

This is large Arial text. One conditional statement (like an if, else, or elif) is placed inside another. This allows testing multiple conditions.

# Prompting the user for their age and preference for sweets
age = int(input("Please enter your age: "))
sweets = input("Do you like sweets? (yes/no): ").strip().lower()

# Using nested conditionals to determine the snack
if age < 18:
    if sweets == "yes":
        print("You get candy")
    else:
        print("You get a fruit snack.")
else:
        print("You can have fruits")
        
You can have fruits

The user is first asked for their age. If under 18, they are asked about their sweet preference. If they are 18 or older, they simply get fruits. If the user likes sweets and is under 18, they get candy. If not, they get a fruit snack, which I did by using if and else conditionals.

# Define the prices of different laptops
laptop1_price = 500  # Budget laptop
laptop2_price = 1000  # Mid-range laptop
laptop3_price = 1500  # High-end laptop

# Prompt the user for their savings
savings = float(input("Enter your savings: $"))

# Nested conditionals to check which laptop(s) the user can afford
if savings >= laptop3_price:
    print("You can afford the high-end laptop.")
elif savings >= laptop2_price:
    print("You can afford the mid-range laptop.")
elif savings >= laptop1_price:
    print("You can afford the budget laptop.")
else:
    print("You cannot afford any laptop right now.")
You can afford the high-end laptop.

The user is first asked to enter their savings by using an input() statement, depending on that if the user has enough savings for the high-end laptop, they’ll be shown that option. If they don’t have enough for the high-end laptop but can afford the mid-range one, they get that option. If they can only afford the budget laptop, they will be informed. If they can’t afford any laptop, they are told as well, which is used with if, elif, and else statements.

%%js
let Money = true;
let FreeTime = true; 

// Check if you can go on a trip
if (Money) {
    if (FreeTime) {
        console.log("You can go on the trip!");
    } else {
        console.log("You have money, but no free time for the trip.");
    }
} else {
    if (FreeTime) {
        console.log("You have time, but not enough money for the trip.");
    } else {
        console.log("You neither have money nor free time for the trip.");
    }
}

<IPython.core.display.Javascript object>

I used nested conditionals to determine whether you can go on a trip based on two factors, money and free time.

%%js
// Get age from the user
let age = parseInt(prompt("Enter your age: "));

// Check ball ownership
let hasBall = prompt("Do you have a ball? (yes/no)").toLowerCase() === 'yes';

// Check eligibility based on age and ball ownership
if (age >= 5 && hasBall) {
    // Determine play group based on age
    let group;
    if (age < 8) {
        group = "under 8 group";
    } else {
        group = "8 and older group";
    }

    // Display result
    console.log(`You can join the game! You are in the ${group}.`);
} else {
    console.log("Sorry, you cannot join the game. You must be at least 5 years old and have a ball.");
}

I used javascript to determine eligibility for joining a game based on age and ball ownership. I used the prompt() statement do determine if they have a ball and there age. Depending on that I used if else conditionals to determine if thet can join the game.