Skip to the content

Nested decisions

Python · Unit 2: Conditionals

An if can go inside another one. The inner one is only checked if the outer one was met.

Each level takes 4 more spaces of indentation.

age = 70
if age >= 18:
    print("Adult")
    if age >= 60:
        print("With discount")

Prints

Adult
With discount

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Predict the output

    What does this code print?

  3. 3. Find the bug

    It should show the panel only to an active admin. Which line has the error?

  4. 4. Multiple choice

    Which condition does the same as these two ifs?

  5. 5. Predict the output

    What does this code print?

  6. 6. Predict the output

    What does this code print?

  7. 7. Complete the code

    At 10 or less the battery is "critical"; under 50 it is "low", and otherwise it is "fine". Complete the function.

  8. 8. Put the lines in order

    Put the function in order. At 85 degrees or more it is "hot", at 60 or more it is "warm" and anything else is "cold".

  9. 9. Multiple choice

    What does this function do?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →