Back Navigation Next Navigation Conceptual Review (page 26 of 55)

Python also supports nesting conditionals where one conditional may be nested within another. This allows you to check multiple conditions in a hierarchical or layered way.

The key with nesting conditionals is to get the indentation correct, so the interpreter knows which blocks of code to execute when the nested expressions are evaluated to True or False. Having the wrong indentation will result in Python getting confused how to match the if-elif-else clauses.

Conditionals

In theory, we can have an unlimited number of nested conditionals, but we want to make sure our code is readable. Therefore, we may consider modifying our code after three or four nested levels of conditionals to atempt to find a simpler solution.