0

My first real program

  1. stars = 0
    repeat 3:
        move_right()
        collect()
        stars = stars + 1
    say(stars)

    A whole program

    Now put it all together. This program moves the robot and counts stars. Read it one line at a time. Keep track of the robot and the box.

    1. 1stars = 0
      box holds 0
    2. 2repeat 3:
      move, collect, add 1
    3. 3say(stars)
      says 3

    Line by line

    Line 1 puts 0 in the stars box. The loop moves, collects and adds 1, three times. The last line says how many stars: 3.

  2. set stars to 0
    repeat 3 times
    move right
    collect the star
    add 1 to stars
    say stars

    Watch the robot

    Press Run. The robot moves right 3 squares and collects a star each time. It adds 1 for each star. That is why it says 3.

  3. stars = 3
    if stars > 2:
        say("You win")
    else:
        say("Try again")

    Add a choice

    The sign > means more than. stars holds 3. Is 3 more than 2? Yes, so the robot says You win. With 1 star, it would say Try again.

  4. repeat 3:
    says 3
    repeat 5:
    says 5

    Change the program

    To change what it says, change the right part. Lily wants the program to say 5. Change repeat 3: to repeat 5:. Now the loop adds 1 five times.

For parents

Goal: Your child reads a whole program with a variable, a loop and a choice, and says what it will do.

  • Make a small table on paper with three columns: line, robot, box. Fill it in together.
  • Ask your child to explain what each line does before giving the final answer.
  • The last lab is the big end of year challenge. It uses a loop, if, else and a counter together. Let your child plan it on paper first, and cheer every try.

Away from the screen: Write a short program on paper for a real task, like steps = 0, repeat 5: step forward and add 1, say steps. Your child acts it out and says the number.

Coding · Variables and a First Program
Back to top