RBI Chapter 6

From IPRE Wiki
Revision as of 17:48, 14 May 2008 by Mansi (Talk | contribs)

Jump to: navigation, search

Chapter 6: Loop(If Else & Again, 2)

Things You Will Learn

  • Writing If-Else Statements
  • Writing Loops

Remember the game we played last class with the board game? Let’s do another example. Suppose you want to divide the class into boys and girls. Can you think of a way to do it? You would have to set a condition such that if you are a girl, you should say, “I am a girl”, else you should say, “I am a boy.”

Here is how you can do it:

if person == girl:
	speak(“I am a girl”)
	speak(“I am a boy”)

In order to add these, we can simply say:

>>> x + y

Notice the first line of the code: there is a double “equal to” sign. Whenever we want to compare a variable to something, we use the double “equal to” sign. In this case we were checking if the person is a girl. Hence, we wrote: if person == girl. We use “=” to assign values to variables, as you have been doing.

Whenever there is an “if” in your code, you must have an “else:” or “elif:” (which stands for else if) following the “if.” Let us look at an example where we use “elif.” Suppose we want to separate the blondes, brunettes, red-haired and people with any other hair color in the classroom and we want each person to say which group they fall into. How will the code for it look?

if person == blonde:
	speak (“I am blonde”)
elif person == brunette:
	speak (“I am brunette”)
elif person == redhaired:
	speak (“I am red-haired”)
	speak (“I am not any of those”)

Then you decide that your robot is going too slow and you want to increase the speed to 0.8. In order to do this, you will have to go through all 5 steps and change 0.5 to 0.8. A better way would've been to write it this way:

x = 0.5
forward(x, 1) 
turnLeft(x, 2)
turnRight(x, 5)
forward(x, 10)
turnRight(x, 7)

Now can you see the difference between else and elif? In the first example, we used else instead of elif because there were only two options. Look at the third line of the first example again. It says:

	speak(“I am a boy”)

instead of

if person == boy:
	speak(“I am a boy”)

This means that if the person is not a girl, then that person has to be a boy. There are no other conditions. So writing only “else” is enough. However, in our second example, if a person is not blonde, he/she can have black hair or red hair or hair of some other color. It has more than two conditions. So we need to have the “elif” statements to take those other options into consideration.

Loops: Redraw the Square
ARemember what you learned about loops last class? Let's apply the idea to an activity you have done before. After doing this, you will understand the importance of loops!

We have already taught you how to put your commands in a file so that they will run in a sequence. Remember how you made your robot walk like a guard in the second class? You wrote something like this:

from myro import *

forward(4, 0.5)
turnRight(1, 0.6)

forward(4, 0.5)
turnRight(1, 0.6)

forward(4, 0.5)
turnRight(1, 0.6)

forward(4, 0.5)
turnRight(1, 0.6)

forward(4, 0.5)
turnRight(1, 0.6)

Repeating commands like this is very inefficient. What if you could write one command which would tell it to repeat a command, or a number of commands, 5 times? Fortunately, there is a way to do that – using loops. It's very simple. First we need to decide what we are repeating. In this example, it is repeating the following two lines of commands:

forward(4, 0.5)
turnRight(1, 0.6)

We can give this sequence a name: let’s call it step. This means that you have defined the sequence as step. To do this, you must start with def step(), so it will look like this:

def step():
	forward(4, 0.5)
	turnRight(1, 0.6)

You have just defined a function! A function defines a sequence of commands that you want your computer or robot to follow.

Note: While in this example, the function is called “step”, you can call it anything you want.

Next, you want to tell your robot to repeat step() 5 times. To do this, you can use loop() in the following way:

>>> loop(function, number of repetitions)

In this case, you would write:

>>> loop(step, 5)

Easy? How would you make your robot draw a square in this manner? You want to figure out what you need to repeat in order to make a complete square (remember what you did in Robot Review!).

How would you make a triangle?

Activity: Maze
Now you will use your knowledge of if-else statements to write a program of your own to steer your robot through a maze set up for you in your classroom. You will have to use if-else statements to find your way through the maze to the end.

So what code do you think you will need to write to make this work and how will you write it? Think about what are the main challenges are in moving through a maze. When your robot is moving forward and it faces an obstacle, you can ask it to turnLeft and move forward again. Or you can ask it to turnRight instead. It can be done in many different ways. For example, you can define a function called walk in the following way:

def walk():
	if wall():
		if heads():
			turnLeft(0.7, 0.2)
			turnRight(0.7, 0.2)

Is that too complicated? Don’t worry; you know enough to understand it!

def walk() is the definition of this function that we have named walk. Remember, you can name it whatever you want. heads() is a function which models the flipping of a coin. When you flip a coin, 50% of the time, you will get heads and the other 50% of the time you will get tails. Thus, heads() returns True 50% of the time and False the other 50% of the time. In the above example, if heads() is True, then the robot will turnLeft, else it will turnRight. This is just to randomize your robot’s movement, so that it will turnLeft half the time and turnRight half the time. If your robot does not see a wall, it will continue going forward. Easy, right? You can change the above code to other similar commands and values that you have learned.

So what happens when you run this code? Your robot will move forward if there is no wall, or stop and turnLeft or turnRight when it sees a wall, and then it stops. What should we do to make it repeat itself? That’s right, we should use loops! You can repeat the walk function 100 times by typing:

>>> loop (walk, 100)

Good luck!

Chapter 1.
Chapter 2.
Chapter 3.
Chapter 4.
Chapter 5.

CREU Project Home.