Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Nested Conditions

A block of code can contain any code, even another if statement. An if inside the block of another if is called a nested condition. It's useful when one question only makes sense after another. At the movies, for example, we first check whether the visitor has a ticket, and only then how old they are:

has_ticket = True
age = 15
if has_ticket:
    if age >= 18:
        print("Enjoy the movie")
    else:
        print("Sorry, this movie is for adults only")
else:
    print("Please buy a ticket first")

This prints:

Sorry, this movie is for adults only

The inner if statement runs only when has_ticket is true. Its blocks are indented by 4 more spaces, 8 in total, and each else is indented as much as the if it belongs to. That's how Python knows that the first else belongs to if age >= 18 and the second one to if has_ticket.

Nested conditions can often be rewritten with and and elif. This code does the same:

has_ticket = True
age = 15
if has_ticket and age >= 18:
    print("Enjoy the movie")
elif has_ticket:
    print("Sorry, this movie is for adults only")
else:
    print("Please buy a ticket first")

This prints:

Sorry, this movie is for adults only

The program gets to elif has_ticket only when the first condition is false. With a ticket, that means the visitor is under 18. Neither way is wrong: pick the one that's easier to read. When neither if has an else, a single if with and usually reads better: instead of if has_ticket: with if age >= 18: inside it, write if has_ticket and age >= 18:. It does the same, with one level of indentation less.

Instructions

  1. 1

    A student passes the exam with 50 points or more, and with 75 or more passes with distinction. In the block of if points >= 50:, write a nested if statement with the condition points >= 75 that prints Passed with distinction, and give it an else branch that prints Passed. You can reuse the line print("Passed"): move it into the inner else and indent it by 4 more spaces.

  2. 2

    Change points to 82 and run the program. It should print Passed with distinction.

Back

14/15

  1. 1. The if Statement
  2. 2. Blocks of Code
  3. 3. The else Branch
  4. 4. Quiz: if and else
  5. 5. Comparison Operators
  6. 6. Quiz: Arranging a Condition
  7. 7. Boolean Values
  8. 8. More Options: elif
  9. 9. Quiz: elif
  10. 10. Logical Operators: and
  11. 11. Quiz: and
  12. 12. Logical Operators: or
  13. 13. Logical Operators: not
  14. 14. Nested Conditions
  15. 15. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.