Nested Conditionals

Nested conditionals build on the If-Then-Else concepts we taught in Lesson 3.6.

  • Conditional Statements: Used to perform different actions based on different conditions.
  • Nested Conditionals: A conditional statement inside another conditional statement.

Nested Conditionals Code Example in Python

```IF condition1 {

// Actions to run for condition 1
// CODE BLOCK A

IF condition2 
{ //within condition 1 
    // Actions to run for condition 2
    // CODE BLOCK B
}
ELSE 
{
    //Actions to run if condition 2 is not met
    // CODE BLOCK C
} } ELSE {
//Actions to run if condition 1 was never met
// CODE BLOCK D } ```

This code states that

  • If condition 1 AND condition 2 are met, run code blocks A and B
  • If condition 1 is met but condition 2 IS NOT met, run code blocks A and C
  • If condition 1 was never met, run code block D

Example 1 - Basic

  1. Write a code that uses information about the weather, boots, and transportation
  2. Determine whether or not to go hiking
  3. Give solutions for what to do during each type of weather

weather = "sunny"
transportation = "available"
boots = "not present"

print("The weather is " + weather)
print("Your transportation is " + transportation)
print("Your boots are " + boots)


if weather == "sunny":
    if transportation == "available":
        if boots == "present":
            print("You are ready to go hiking!")
        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 not present
You need to find your boots first.

Popcorn Hack 1

Try adding an additional condition for hiking (like location_determined or miles_in_the_hike).

Example 2 - Booleans

  1. Write a code that checks if you can host movie night.
  2. Use the presence of a living room, a projector, and snacks to determine if movie night is possible
  3. Use booleans in the code
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.

Popcorn Hack 2

Try changing some of the defined booleans and see what values you get!

Example 3 - Integers, Booleans, AND Random

  1. Write a code that checks if you can go on a spontaneous road trip
  2. Check if you have enough gas, if the weather is good, if you’ve checked your car, and if you have enough in your budget
  3. Use integers, booleans, and randomized values
import random

# Boolean: Do you have enough gas? (Fixed)
enough_gas = True

# Random: Is the weather good? (Random outcome)
good_weather = random.choice([True, False])

# Random: Did the car pass the check-up? (Random outcome)
car_checked = random.choice([True, False])

# Integer: Your current budget (Random between $50 and $200)
budget = random.randint(50, 200)
required_budget = 100  # Assume you need $100 for the trip

# Check the conditions using nested conditionals
if enough_gas:
    print("You have enough gas!")
    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:
    print("You need to fill up the tank first.")

You have enough gas!
The weather isn't good for a road trip.

Popcorn Hack 3

Try changing the enough_gas variable to a randomly-generated value! Also, try adding a gas value and check that the number of gallons is sufficient.

Learn more about Python Nested Conditionals!

Python Nested Conditionals - Geeks for Geeks