nesting 'if' statements?

Delaney, Timothy tdelaney at avaya.com
Mon May 21 20:41:24 EDT 2001


> Hey guys,
> Just a quick question:
> 
> Is it possible to nest 'if' statements in python? If so how is it
> accomplished? Can you provide sample code?

Sample code provided ...

> if expression:
>     statements
>     if expression:
>         statements
>     elif expression:
>         statements
>     else:
          statements
> elif expression:
>     statements
>     if expression:
>         statements
> else:
>     statements

Why didn't you just try exactly what you typed here (but with real things
replacing the placeholders)? You only had one mistake - you must have at
least one statement at each indent level (e.g. you can't be missing a
statement under the "else:" - either don't have the else, put something
there, or put "pass" as the statement).

Tim Delaney




More information about the Python-list mailing list