powered by TinkerSpace.in
Join free
TinkerPy Tutorial 📘 Part 1 · Starter · Chapter 5 of 24

Decisions and Loops

Smart gadgets decide (“if the button is pressed, ring the bell”) and repeat (“beep 3 times”, “wait until someone presses start”). With if, repeat, for and while your board starts to think — and saves you from typing the same lines again and again!

📖Key words

if / elif / else

if asks a yes / no question; its indented lines run only when the answer is yes. elif (“else if”) asks another question. else runs when every answer was no.

repeat(n):

Runs the indented lines exactly n times, then the program moves on.

for i in range(n):

Runs the lines n times and counts in i: 0, 1, 2 … up to n − 1.

while condition:

Repeats the indented lines as long as the condition is yes — like “wait while the start button is OFF”.

break

Jumps out of the repeat, for or while it is in, straight away.

🎬See it work

The decision path

Hold the button and watch the program choose its path: the if lines or the else lines.

Count the rounds

Pick repeat(3) or for i in range(5), press Run and count every round.

🧠How it works

if, elif and else

  • if button == ON: → is the button pressed? Note the two equals signs (==) — they mean “is it equal to?”.
  • The lines indented under if run only when the answer is yes; else: lines run when it is no. else is optional.
  • elif asks a second question. The board checks from the top and runs only the first block that says yes.
  • if, elif and else lines end with a colon, and their lines are indented 4 more spaces.
  • Inside loop:, the question is asked again and again, so the gadget reacts the moment you press.

repeat and for

  • repeat(3): → do the indented lines exactly 3 times, then carry on. The number can be a variable too.
  • for i in range(5): → i takes the values 0, 1, 2, 3, 4: five numbers, starting at 0, stopping before 5.
  • Use i inside the loop, for example to change a brightness each round (chapter 6).
  • Remember: loop: already runs forever; repeat and for run a set number of times inside it.

while and break

  • while start == OFF: → keep repeating while the answer is yes. Here: wait until the start button is pressed.
  • Put a short wait() inside a while that waits for a button or sensor.
  • break leaves the nearest repeat, for or while early — like a stop button in the middle of a show.
  • Don’t write while True: to run forever — loop: already does that.

Blocks inside blocks

  • You can put a repeat inside an if, an if inside a for, and so on. Each new block is indented 4 more spaces.
  • Three-beep alarm: if the button is pressed → repeat(3) a beep.
  • Indentation decides what belongs where. A line moved left by mistake runs after the loop, not inside it.
  • Test in ✨ AI Simulation: every if line shows a yes / no badge, so you see which path the program took.

💻Code

# Add the libraries you need
import buzzer

# Give your pins easy-to-remember names
i1 = set_name("button")
o1 = set_name("bell")

# ===== Doorbell =====

# This code block executes only once
setup:
    button = set_input(push_button, active_low)
    bell = set_output(buzzer)

# This code block executes forever
loop:
    if button == ON:  # when button is ON
        bell = ON  # turn bell ON
    else:  # otherwise
        bell = OFF  # turn bell OFF

A doorbell with if and else: while the button on i1 is pressed the bell (a buzzer on o1) rings, otherwise it is quiet. Because this is inside loop, the board asks the question again and again.

# Give your pins easy-to-remember names
i1 = set_name("player1")
i2 = set_name("player2")
o1 = set_name("light1")
o2 = set_name("light2")

# ===== Quiz buzzer =====

# This code block executes only once
setup:
    player1 = set_input(push_button, active_low)
    player2 = set_input(push_button, active_low)
    light1 = set_output(led)
    light2 = set_output(led)

# This code block executes forever
loop:
    if player1 == ON:  # when player1 is ON
        light1 = ON  # turn light1 ON
        wait(3000)  # keep the winner’s light on for 3 seconds
        light1 = OFF  # turn light1 OFF
    elif player2 == ON:  # otherwise, when player2 is ON
        light2 = ON  # turn light2 ON
        wait(3000)  # pause here for 3000 milliseconds before continuing
        light2 = OFF  # turn light2 OFF

Quiz buzzer for two players with if and elif: whoever presses first lights their LED for 3 seconds. If both press at the same moment, only the first block (player 1) runs — the board checks from the top. No else is needed here.

# Add the libraries you need
import buzzer

# Give your pins easy-to-remember names
i1 = set_name("button")
o1 = set_name("beeper")

# ===== Three-beep alarm =====

# This code block executes only once
setup:
    button = set_input(push_button, active_low)
    beeper = set_output(buzzer)

# This code block executes forever
loop:
    if button == ON:  # when button is ON
        repeat(3):  # repeat the steps below 3 times in a row
            beeper = ON  # turn beeper ON
            wait(200)  # pause here for 200 milliseconds before continuing
            beeper = OFF  # turn beeper OFF
            wait(200)  # pause here for 200 milliseconds before continuing

Three-beep alarm: a repeat inside an if. One press of the button gives exactly three beeps (on 200 ms, off 200 ms each).

# Give your pins easy-to-remember names
i1 = set_name("start")
i2 = set_name("stop")
o1 = set_name("lamp")

# ===== Start and stop show =====

# This code block executes only once
setup:
    start = set_input(push_button, active_low)
    stop = set_input(push_button, active_low)
    lamp = set_output(led)

# This code block executes forever
loop:
    # wait here until the start button is pressed
    while start == OFF:  # keep repeating the steps below while start is OFF
        wait(20)  # pause here for 20 milliseconds before continuing
    for i in range(10):  # repeat the steps below 10 times, counting each time in i
        lamp = ON  # turn lamp ON
        wait(300)  # pause here for 300 milliseconds before continuing
        lamp = OFF  # turn lamp OFF
        wait(300)  # pause here for 300 milliseconds before continuing
        if stop == ON:  # when stop is ON
            break  # stop this loop now and go on after it

while and break together: the program waits in the while until start (i1) is pressed, then blinks the lamp up to 10 times with a for loop. Holding stop (i2) when a blink ends breaks out of the for loop early — the board checks stop once per round. Then loop starts again and waits for start.

Open TinkerPy Code

🎉Fun examples

🚪Automatic doors

If someone is near → open. Else → stay closed. Every automatic door at a mall or airport runs an if / else.

🏃PT class loops

“Do 10 jumping jacks” is a repeat(10) for humans. “Run until the whistle blows” is a while!

🚀Countdown

Rocket launches count down 10, 9, 8 … 1 — a loop with a counter, ending with a big “liftoff!”.

🌍Where it’s used

🛠️Project ideas

Quiz buzzer for two players

Two buttons (i1, i2) and two LEDs (o1, o2), named player1, player2, light1 and light2. If player 1 presses, light 1 glows for 3 seconds; elif player 2 presses, light 2 glows. Add a buzzer on o3 that beeps once for every winner.

🧰 You need: 2 push buttons, 2 LEDs, a buzzer, TinkerPy board or Simulator
Three-beep alarm

A button on i1 and a buzzer on o1. When the button is pressed, beep exactly 3 times with repeat(3). Then change it to a for loop that beeps 5 times, a little longer each time.

🧰 You need: push button, buzzer, TinkerPy board or Simulator

⭐Points to remember

  1. if asks a yes / no question.
  2. == means “is equal to”; = gives a value.
  3. else runs when the if answer is no.
  4. elif asks another question; only the first yes block runs.
  5. if, elif, else, repeat, for and while lines end with a colon.
  6. repeat(n): runs exactly n times.
  7. for i in range(n): counts i from 0 to n − 1.
  8. range(5) gives 0, 1, 2, 3, 4.
  9. while repeats as long as its question is yes.
  10. Put a short wait() inside a while that waits for a button.
  11. break jumps out of the loop it is in.
  12. loop: already runs forever — no while True: needed.
  13. Blocks inside blocks are indented 4 more spaces.
🤯
Did you know?

A self-driving car makes a huge number of if-style decisions every second: if a person is ahead, slow down; if the light is red, stop…

❓Mini quiz

🔑
Log in to try the mini quiz

5 quick questions to check yourself — free with your TinkerPy account.

Log in