Learn to program

 
  Finch assignments ::

Learning Goal: Understand how to write a program to control a robot, using loops, conditionals and subroutines where appropriate.

 

 
 

Finch Assignment 11 - Loops

Learning goal: understand that loops can simplify programs.

FOR LOOPS:

If you want to repeat the yoyo behavior 10 times, all you have to do is:

for i in range (10):
                yoyo()

This is a new statement in Python – the for statement. It is also called a loop. This is a way of repeating something a fixed number of times.

This is the basic syntax:

for  <variable> in <sequence>:
                <do something>

<do something>

Try the program:

for i in range(10):
         print(i)

Run it and see what happens.

You should see this:

0
1
2
3
4
5
6
7
8
9

Notice that you get 10 numbers, from 0 through 9. The variable i in the loop takes on the values 0 through 9 and does the commands in the block for each value.

 

WHILE LOOPS:

Watch this video to find out about While Loops. It will also give you a new perspective on If loops.

 

Assignment 11:

Using a loop, write a short program for the robot to travel along a square route.
Save to Shared - Student - Pereira - AI 201x - Class color - Finch assignments - 11 Loops as squareyourname.


Write a Reflection on what you have learned.