Nested Conditionals | BUS 104 – Python Exam

Still feeling nervous for your exam? Check out the Python Cram Kit for practice questions that emulate the difficulty that you'll face on your exam. Each question includes a thorough step-by-step explanation where I walk you through the entire problem solving process!

Nested conditionals explained

In order to learn what nested conditionals actually are, check out this template code:

if condition:
    if condition:
        code to be executed if condition is true

Notice how another if statement occurs within a different if statement?

if condition:
    if condition:
        code to be executed if condition is true

That's the nested conditional. It's nested within another if statement.

Nested conditionals are conditional(s) that occur within the code to be executed of another conditional statement.

Coding a nested conditional

Check out this Python program that determines if someone can legally drink:

if age > 21:
    print("You can drink legally.")
elif age > 18:
    if location == "Europe":
        print("You can drink legally.")
    elif location == "US":
        print("You cannot drink legally.")

There's 3 potential situations here.

1) The user is above 21 years old...

if age > 21:
    print("You can drink legally.")
elif age > 18:
    if location == "Europe":
        print("You can drink legally.")
    elif location == "US":
        print("You cannot drink legally.")

...and can drink legally.

if age > 21:
    print("You can drink legally.")
elif age > 18:
    if location == "Europe":
        print("You can drink legally.")
    elif location == "US":
        print("You cannot drink legally.")

2) The user is above 18 years old...

if age > 21:
    print("You can drink legally.")
elif age > 18:
    if location == "Europe":
        print("You can drink legally.")
    elif location == "US":
        print("You cannot drink legally.")

...and in Europe, so they can drink legally.

if age > 21:
    print("You can drink legally.")
elif age > 18:
    if location == "Europe":
        print("You can drink legally.")
    elif location == "US":
        print("You cannot drink legally.")

3) The user is above 18 years old...

if age > 21:
    print("You can drink legally.")
elif age > 18:
    if location == "Europe":
        print("You can drink legally.")
    elif location == "US":
        print("You cannot drink legally.")

...and in the US, so they cannot drink legally.

if age > 21:
    print("You can drink legally.")
elif age > 18:
    if location == "Europe":
        print("You can drink legally.")
    elif location == "US":
        print("You cannot drink legally.")

Notice how in situations #2 and #3, we enter the same else if statement...

if age > 21:
    print("You can drink legally.")
elif age > 18:
    if location == "Europe":
        print("You can drink legally.")
    elif location == "US":
        print("You cannot drink legally.")

...verifying the user is above age 18? This is the power of nested conditionals.

Nested conditionals enable you to go a step further in determining the desired output for a user.

One last nugget of gold before moving on. If you ever find yourself confused in a nested conditional block...

Pro tip: Focus on one block at a time.

I know it sounds obvious, but you'd be surprised how easy it is to see a massive conditional block and get too overwhelmed to even begin understanding what's going on.

Python Follow-along Guide

It's no secret you retain info better when you write it down. That's why I created the Python Follow-along Guide for you!

As you come upon key concepts highlighted in yellow, like this sentence here, you can fill-in-the-blanks on your Python Follow-along Guide so that you remember all the important stuff for later!

You can obtain your Python Follow-along Guide for FREE by entering your email below!

Next, let's dig into flag variables and how we can utilize them with loops. Click below to continue learning!

Concepts

Each exam concept broken down with relatable situations to your life!

ToolsPython Follow-along Guide (FREE)
LessonPrint function
LessonInput function
LessonConcatenation
LessonConditionals (if, elif, else)
LessonWhile loops
LessonFor loops
LessonData Types
LessonNested Conditionals
LessonFlag Variables

Practice Problems

Include step-by-step explanations through each part of the problem!

Python Cram Kit Logo

"...I was blown away with the videos and the ability to explain what is going on at a simple level. Highly recommend!"

Jack Whelan
March 8, 2021

Rating: 5 out of 5.

Leave a Comment