RBI Chapter 3

From IPRE Wiki
Jump to: navigation, search

Chapter 3: Mission Variation

Things You Will Learn

  • What variables are.
  • How the robot speaks and takes pictures.


Variables
A variable is a letter or word used as a symbol to represent a number, words, picture,s etc. A variable can do exactly what its name suggests – it can store a value which can change. In math, usually letters such as x, y, a, b, s, h, etc. are used as variables. For example, we can type:

>>> x = 5 
>>> y = 6 

In order to add these, we can simply say:

>>> x + y
11

Similarly, we can divide, multiply, subtract, etc. You must be thinking why we cannot simply say 5+6 or 5/6, etc. Suppose you want your robot to go forward, turn left, turn right, go forward again, and finally turn right, all at the same speed. You may write it this way:

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

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, if you want to change the speed, all you have to do is say x = 0.8. If you wanted the time to be the same, it would be a good idea to store it in another variable so that you can change it just as easily. How would you change the above commands to do this?

Talker Scribbie
As you saw in the first class, your robot has the ability to speak. You can make your robot say anything you want. If you want to make your robot say “Hello, my name is Scribbie”, simply type:

>>> speak(“Hello, my name is Scribbie”)

Note: The sentence you want your robot to speak has to be in quotes.

You can also store what you want your robot to say in a variable:

>>> phrase = “Hello, my name is Scribbie”
>>> speak(phrase)

Did you notice that phrase was not written in quotes? Try putting it in quotation marks and see what happens. Did it say “Hello, my name is Scribbie”? Why not? Because phrase is a variable which stores “Hello, my name is Scribbie”. So, when using variables, the quotation marks do not need to be included.

Similarly, you can make your robot do the following calculation and it will reply with the answer:

>>> x = 9
>>> y = 5
>>> speak(x-y)

Here your robot will say "4", since 9 - 5 = 4.

Photographer Scribbie
In the first class, you drove your robot to Mars and took a picture of Martians using the game pad. Let's now take pictures by writing commands. This is very simple:

>>> takePicture()

Can you see the picture you took? Probably not, since you haven't stored it...in a variable! You can call your picture anything. In this example, we will call it pic.

>>> pic = takePicture()

Now if you want to see the picture, type:

>>> show(pic)

If you want to save your picture so that you can view it later, you can type:

>>> savePicture(variable, “filename.jpg”)

where variable is where you saved the picture (pic in this case) and filename.jpg is whatever name you want to call the picture after you save it on your computer.

>>> savePicture(pic, “mypic.jpg”)

Note: .jpg is a type of picture file, but you don't need to worry about it.



Chapter 1.
Chapter 2.
Chapter 4.
Chapter 5.
Chapter 6.
Chapter 7.
Chapter 8.

CREU Project Home.