# Learning Python 3 with the Linkbot/Decisions
### If statement

As always I believe I should start each chapter with a warm-up typing exercise, so here is a short program to compute the absolute value of an integer:
Here is the output from the two times that I ran this program:
So what does the computer do when it sees this piece of code? First it prompts the user for a number with the statement "n = int(input("Number? "))". Next it reads the line "if n < 0:". If n is less than zero Python runs the line "print("The absolute value of", n, "is", -n)". Otherwise it runs the line "print("The absolute value of", n, "is", n)".
More formally Python looks at whether the expression n < 0 is true or false. An if statement is followed by an indented block of statements that are run when the expression is true. Optionally after the if statement is an else statement and another indented block of statements. This second block of statements is run if the expression is false.
There are a number of different tests that an expression can have. Here is a table of all of them:
| operator | function |
| < | less than |
| <= | less than or equal to |
| > | greater than |
| >= | greater than or equal to |
| == | equal |
| != | not equal |
Another feature of the if command is the elif statement. It stands for else if and means if the original if statement is false but the elif part is true, then do the elif part. And if neither the if or elif expressions are true, then do what's in the else block. Here's an example:
and the output:
Notice how the elif a <= 3 is only tested when the if statement fails to be true. There can be more than one elif expression, allowing multiple tests to be done in a single if statement.

### Moving a Linkbot's Motors

In the previous chapter, Who Goes There?, we saw that you can move a linkbot's motor with the moveJoint function. It is typically used like:
where the first argument is the motor you want to move and the second argument is the angle in degrees you want to move it. The above code will move motor "1" in the positive direction by 180 degrees.
What if you want to move more than one motor at a time? The linkbot also has a move function that does just that. It looks something like this:
The move function expects three numbers inside the parentheses. The first number is the angle to move motor 1, the second number is the angle to move motor 2, and the third number is the angle to move motor 3. If your Linkbot only has two motors, the value for the face that does not have a motor is ignored. For example, if I have a Linkbot-I and I ran the above sample code, the "180" is ignored because motor 2 is not a movable motor. Instead, the Linkbot will move motor 1 forward 90 degrees and motor 3 forward 270 degrees.
When the move function is executed, the Linkbot will move any/all of its motors simultaneously.

#### command_linkbot.py

In this example, we'd like to create a program that we can use to move our Linkbot around. This demo will use the python break command. In the previous chapter, we saw that Python can get stuck in "infinite loops". The break command can be used to exit loops even if the loop condition is still true.
Lets start with just moving the Linkbot forward and backward first. To run this demo, you'll need a Linkbot-I and two wheels. A caster is also recommended but optional.
A Note on Strings
You might have noticed that towards the end of the program, there is a line that reads print('I\'m sorry. I don\'t understand that command.') with funny backslashes in the code. When you run the program though, it doesn't print those backslashes. These backslashes actually serve an important purpose.
When Python encounters either a single quote ' or double quote " , it knows that what follows will be a string that is terminated by the same type of quotation mark that started it. But, what if we want to include some quotation marks as part of the string? Consider the following piece of code:
When Python sees the first Quotation mark at "Hello th..., it says "Ok, this is going to be a string and as soon as I see another quotation mark, that will be the end of the string". Soon, Python sees another quotation mark at ...says "woof! and thinks "I've found the end of the string!", but this is not the end of our string! We wanted Python to go all the way to the end at ...bored." . How do we tell Python that the quotation marks around "woof!" are special, and that our string doesn't actually end there?
The answer is backslashes. If there is a backslash preceding a quotation mark, that is a special indicator to Python to include that quotation mark as part of the string, rather than terminating the string. Therefore, the fixed code should be:
In this example, the single quote in he's is automatically included as part of the string because the string started with a double-quotation mark, and thus Python is looking for a double-quotation mark to terminate the string.
Sample runs:

### Examples

equality.py
And the output
high_low.py
Sample run:
even.py
Sample runs:
average1.py
Sample runs:
average2.py
Sample runs:

### Exercises

Write a program that asks the user their name, if they enter your name say "That is a nice name", if they enter "John Cleese" or "Michael Palin", tell them how you feel about them ;), otherwise tell them "You have a nice name."
Modify the higher or lower program from this section to keep track of how many times the user has entered the wrong number. If it is more than 3 times, print "That must have been complicated." at the end, otherwise print "Good job!"
Write a program that asks for two numbers. If the sum of the numbers is greater than 100, print "That is a big number."
Modify the Linkbot program presented earlier to include the commands "turnright", "turnleft", and "beep". Make the "turnright" command turn the robot to the right, "turnleft" turn the robot to the left, and "beep" beep the robot buzzer for 1 second.