CoursesAIMathCodePhysicsAbout and FAQ

TwiddleLab / Code / How a Programmer Thinks

Decisions: if

Lesson 3 of 10 · 7 min · Free, no account needed

What you will learn

  • A condition like battery < 30 is always True or False (a boolean).
  • if runs its indented lines only when the condition is True; else runs when it is False.
  • elif asks a second question, only if the first answer was False.
  • Edges matter: < 13 and <= 13 differ by exactly one value.

Start the lesson

How the lesson goes

  1. Same robot, same program. On Monday it went to play.

    Today its battery is at 20%. Tap where you think it goes.

  2. Find the exact level where it changes its mind.

    Drag the battery. Stop at the highest level where the robot still goes to charge.

  3. Step through it for both days.

    When the condition lights up, tap the line you think runs next.

  4. The parts of a decision have names.

    A condition is a question with a yes or no answer, like battery < 30. Python answers True or False. These two values are booleans.

    if runs the indented lines under it only when its condition is True. else runs when it is False. Note the colon and the indent.

    elif means "else if": a second question, asked only when the first one was False. == asks "is it equal?".

    print shows text on the screen. Watch this one run with battery 50.

  5. Charge below 30, dance at exactly 100, play otherwise.

    Put the lines in order. Run tests your code on three battery levels.

  6. battery is 20. Light the right lamp for each condition.

    Drag each condition into True or False.

  7. Gatekeeper: this site is for 13 and older.

    Build the gate's rule from tiles, then open the gate for the queue. Get all 10 right.

  8. Bug report: a 13-year-old can't sign up.

    The rule says 13 and older may join. Tap the bug, fix it, then Run.

  9. battery is 30. Which line prints?

    Tap the print line that runs.

  10. Make a card of your first rule.

    Drag the rule you built for the gate onto the card.

Previous lesson: Variables Are Boxes · Next lesson: Bugs Are Clues · Part of the course: How a Programmer Thinks

Lessons are interactive and need JavaScript. Turn it on to play them.

Loading TwiddleLab