## Python variables and commenting – Driving Lesson #3

• Pre-driving checks

If you are here you should be able use an editor to write Python code to control  your Pi-Car. If not by all means have a go at this lesson but if you get stuck try Driving Lesson#1 or Driving Lesson#2.

• What’s in this driving lesson?

In this lesson we will be considering things called variables and comments. As with the last lesson you may get to the end and still wonder why you need them and why they are important. However, stick with it, when you get to the next Driving lesson and the start of logic it will become clearer.

• A little theory

We will start with a bit of theory again, so most of you will probably skip this bit and go down to the actual doing part but do come back if you don’t understand it!

Variables

So what is a variable? In the last Driving Lesson we learnt about a constant. A variable is similar to a constant in that it is something that holds a value but as the name suggests this value can vary.

This means that as the Raspberry Pi works through the instructions in the program one by one a variable can hold a certain value at one stage in the program but a different value at another stage.

As with a constant, in programming you have the variable name on the left side followed by a ‘=’ symbol and then a value on the right side of it. For example:

petrolLeft = 100

This variable might be used to indicate how much petrol is left as your program runs. Here it is being set to 100 initially,  it can then be set to a different value later.

Also pay attention to the variable name, even though it is two words – petrol and left – there is no space between them. This is one of the rules of the Python language – you cannot have spaces in variables. With this variable you will see we put a capital on the second word in order to make it easier to read. This is a common trick to use.

Variable types

Before we get driving we do just need to consider the different types of variables. The one  we used above was obviously a number but we can also have a variable that have a word, or a number of words associated to it. This is referred to as a String – as in a string of letters. If we want to assign a string we need to put speech marks around it so the Raspberry Pi knows what we mean:

carDriver = “Michael”

• Lets get driving…

Enough of the theory (for those of you who read it) let’s try and drive our Pi-Car and use some variables. At the end of the last driving lesson we finished with the following code.

If you don’t know how to get the code onto your Raspberry Pi, you can either copy it typing it into your text file. Or you can copy it in – instructions here.

```import time, RPi.GPIO as GPIO
ON = 1
OFF = 0
FORWARDS=12
BACKWARDS=11
LEFT=13
RIGHT=15

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(FORWARDS,GPIO.OUT)
GPIO.setup(BACKWARDS,GPIO.OUT)
GPIO.setup(LEFT,GPIO.OUT)
GPIO.setup(RIGHT,GPIO.OUT)

GPIO.output(FORWARDS,ON)
print ("Moving forwards after turning on forwards GPIO pin no = ", FORWARDS)
time.sleep(3)
GPIO.output(FORWARDS,OFF)
GPIO.output(BACKWARDS,ON)
print ("Moving backwards after turning on backwards GPIO pin no = ",BACKWARDS)
time.sleep(3)
GPIO.output(BACKWARDS,OFF)

GPIO.output(LEFT,ON)
GPIO.output(FORWARDS,ON)
print ("Moving left after turning on left GPIO pin no = ",LEFT)
time.sleep(3)
GPIO.output(LEFT,OFF)
GPIO.output(FORWARDS,OFF)

GPIO.output(RIGHT,ON)
GPIO.output(FORWARDS,ON)
print ("Moving right after turning on right GPIO pin no = ",RIGHT)
time.sleep(3)
GPIO.output(RIGHT,OFF)
GPIO.output(FORWARDS,OFF)
```

In this code the car should move forwards, wait for a bit, move backwards, wait for a bit, move forwards and left, wait for a bit and then move forwards and right and wait for a bit. It will also print out what it is doing to the screen.

So lets add a variable. We will add a variable that stores how much petrol is left and then reduce it as the car moves which we can then print out. Put it just beneath the constants you created.

```import time, RPi.GPIO as GPIO
ON = 1
OFF = 0
FORWARDS=12
BACKWARDS=11
LEFT=13
RIGHT=15

petrolLeft = 100

```

Now we have our variable let’s print out the value – as we did with the constants:

```print ("Moving forwards after turning on forwards GPIO pin no = ", FORWARDS, " Petrol left = ",petrolLeft)

```

If you can remember we need to add any words we want to print out in speech marks when we do a print and if we want to print out the value of a variable or constant add a ‘,’ and then the variable name.

Add the part above that prints out the Petrol Left value to each print statement in the code. You should now have the following:

```import time, RPi.GPIO as GPIO
ON = 1
OFF = 0
FORWARDS=12
BACKWARDS=11
LEFT=13
RIGHT=15
petrolLeft=100

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(FORWARDS,GPIO.OUT)
GPIO.setup(BACKWARDS,GPIO.OUT)
GPIO.setup(LEFT,GPIO.OUT)
GPIO.setup(RIGHT,GPIO.OUT)

GPIO.output(FORWARDS,ON)
print ("Moving forwards after turning on forwards GPIO pin no = ", FORWARDS," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(FORWARDS,OFF)
GPIO.output(BACKWARDS,ON)
print ("Moving backwards after turning on backwards GPIO pin no = ",BACKWARDS," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(BACKWARDS,OFF)

GPIO.output(LEFT,ON)
GPIO.output(FORWARDS,ON)
print ("Moving left after turning on left GPIO pin no = ",LEFT," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(LEFT,OFF)
GPIO.output(FORWARDS,OFF)

GPIO.output(RIGHT,ON)
GPIO.output(FORWARDS,ON)
print ("Moving right after turning on right GPIO pin no = ",RIGHT," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(RIGHT,OFF)
GPIO.output(FORWARDS,OFF)
```

If you now save this file and run it you should see it print out the same value for petrol left each time it moves which obviously isn’t right as when a car moves it uses up petrol (yes I know they are batteries in a Pi-Car!).

Changing the value of a variable

So what we want to do is change the value of the petrolLeft variable after each time it moves. To do this we could set it to a different value each time. For example we could say each movement reduces the petrolLeft by 20, so after the first one we could assign petrolLeft to 80.

```
GPIO.output(FORWARDS,ON)
print ("Moving forwards after turning on forwards GPIO pin no = ", FORWARDS," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(FORWARDS,OFF)

petrolLeft = 80

```

So when petrol left is printed out the first time it will print out 100, the next time it will print out 80.

Doing this, we have to work out the value each time throughout the file. It would be easier for us to just say lets take 20 off the current value of petrolLeft and we can do this quite easily by typing:

petrolLeft = petrolLeft – 20

What we are doing here is saying the value of petrolLeft should be whatever the value it is at the moment – which at the start of the file is 100 – and then take 20 away from it. This will then mean that petrolLeft will then be assigned the value 80 (100 – 20).

Doing this means we do not have to work out the value ourselves – which will be very useful when things get more complicated.

What you may notice is that you are still typing in the number 20 a number of times always the same value – so why don’t we make it a constant? If we do this it means if we want to change it at some point we will only have to change it in one place.

Lets define the constant PETROL_USED_PER_MOVE with the other constants:

```ON = 1
OFF = 0
FORWARDS=12
BACKWARDS=11
LEFT=13
RIGHT=15
PETROL_USED_PER_MOVE=20

petrolLeft = 100

```

And then use it when we are calculating the petrolLeft value:

petrolLeft = petrolLeft – PETROL_USED_PER_MOVE

So this should mean you now have the following:

```import time, RPi.GPIO as GPIO
ON = 1
OFF = 0
FORWARDS=12
BACKWARDS=11
LEFT=13
RIGHT=15
PETROL_USED_PER_MOVE=20
petrolLeft=100

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(FORWARDS,GPIO.OUT)
GPIO.setup(BACKWARDS,GPIO.OUT)
GPIO.setup(LEFT,GPIO.OUT)
GPIO.setup(RIGHT,GPIO.OUT)

GPIO.output(FORWARDS,ON)
print ("Moving forwards after turning on forwards GPIO pin no = ", FORWARDS," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(FORWARDS,OFF)
petrolLeft = petrolLeft - PETROL_USED_PER_MOVE

GPIO.output(BACKWARDS,ON)
print ("Moving backwards after turning on backwards GPIO pin no = ",BACKWARDS," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(BACKWARDS,OFF)
petrolLeft = petrolLeft - PETROL_USED_PER_MOVE

GPIO.output(LEFT,ON)
GPIO.output(FORWARDS,ON)
print ("Moving left after turning on left GPIO pin no = ",LEFT," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(LEFT,OFF)
GPIO.output(FORWARDS,OFF)
petrolLeft = petrolLeft - PETROL_USED_PER_MOVE

GPIO.output(RIGHT,ON)
GPIO.output(FORWARDS,ON)
print ("Moving right after turning on right GPIO pin no = ",RIGHT," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(RIGHT,OFF)
GPIO.output(FORWARDS,OFF)
petrolLeft = petrolLeft - PETROL_USED_PER_MOVE

print("Finished moving, petrol left = ", petrolLeft)
```

Run your program and you should see it print out that the petrol is decreasing as your car moves. Also note that if you run your program again the petrolLeft variable will be set to 100 again because the Raspberry Pi executes each instruction in the program. It will not remember the value from the last time you ran it.

Adding a different type of variable

In the theory section we also introduced the idea that variables didn’t just have to store numbers they could also store a word these are referred to as Strings. So lets add a string variable where we will store the drivers name which we will called driversName:

```ON = 1
OFF = 0
FORWARDS=12
BACKWARDS=11
LEFT=13
RIGHT=15
PETROL_USED_PER_MOVE=20

petrolLeft = 100
driversName = "Michael"

```

We can now print out the drivers name whenever we want:

```
print ("Current drivers name is ",driversName)

```

If we want to change the drivers name halfway through we can do this by typing in the variable name and assigning it a new value:

```
print ("Current drivers name is ",driversName)
print ("Changing driver now")
driversName = "Minnie"
print ("New driver is",driversName)

```

This will now print out the new drivers name and your code should now look like the following:

```import time, RPi.GPIO as GPIO
ON = 1
OFF = 0
FORWARDS=12
BACKWARDS=11
LEFT=13
RIGHT=15
PETROL_USED_PER_MOVE=20
petrolLeft=100
driversName="Michael"

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(FORWARDS,GPIO.OUT)
GPIO.setup(BACKWARDS,GPIO.OUT)
GPIO.setup(LEFT,GPIO.OUT)
GPIO.setup(RIGHT,GPIO.OUT)

GPIO.output(FORWARDS,ON)
print ("Moving forwards after turning on forwards GPIO pin no = ", FORWARDS," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(FORWARDS,OFF)
petrolLeft = petrolLeft - PETROL_USED_PER_MOVE
print ("Current driver is ",driversName)

GPIO.output(BACKWARDS,ON)
print ("Moving backwards after turning on backwards GPIO pin no = ",BACKWARDS," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(BACKWARDS,OFF)
petrolLeft = petrolLeft - PETROL_USED_PER_MOVE
print ("Current driver is ",driversName)
driversName = "Minnie"
print ("New driver is ",driversName)

GPIO.output(LEFT,ON)
GPIO.output(FORWARDS,ON)
print ("Moving left after turning on left GPIO pin no = ",LEFT," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(LEFT,OFF)
GPIO.output(FORWARDS,OFF)
petrolLeft = petrolLeft - PETROL_USED_PER_MOVE
print ("Current driver is ",driversName)

GPIO.output(RIGHT,ON)
GPIO.output(FORWARDS,ON)
print ("Moving right after turning on right GPIO pin no = ",RIGHT," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(RIGHT,OFF)
GPIO.output(FORWARDS,OFF)
petrolLeft = petrolLeft - PETROL_USED_PER_MOVE
print ("Current driver is ",driversName)

print("Finished moving, petrol left = ", petrolLeft)
```

When you run the code you should see the petrol reducing as the car moves and the name of the driver changing.

The final thing in this lesson is to learn about comments. When looking at computer code it is sometimes quite difficult to understand. You can make it easier by adding something called comments to your code.

These are lines that you can add which are only for people looking at it and are not meant for the Raspberry Pi to try and do anything with. However, to make sure the Raspberry Pi does not do anything with the line we type we have to tell it not to look any further. We do this by adding a ‘#’ (hash) symbol.

```#make petrolLeft it's own current value less a fixed amount
petrolLeft = petrolLeft - PETROL_USED_PER_MOVE
```

The # symbol means that the Raspberry Pi will not bother looking at the rest of the line and will skip onto the next one so you can type what you like.

```import time, RPi.GPIO as GPIO

ON = 1
OFF = 0
FORWARDS=12
BACKWARDS=11
LEFT=13
RIGHT=15
PETROL_USED_PER_MOVE=20

#variables
petrolLeft=100
driversName="Michael"

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(FORWARDS,GPIO.OUT)
GPIO.setup(BACKWARDS,GPIO.OUT)
GPIO.setup(LEFT,GPIO.OUT)
GPIO.setup(RIGHT,GPIO.OUT)

#Move car forwards
GPIO.output(FORWARDS,ON) #turn pin on
print ("Moving forwards after turning on forwards GPIO pin no = ", FORWARDS," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(FORWARDS,OFF) #turn pin off
#make petrolLeft it's own current value less a fixed amount
petrolLeft = petrolLeft - PETROL_USED_PER_MOVE
print ("Current driver is ",driversName)

#Move car backwards
GPIO.output(BACKWARDS,ON)
print ("Moving backwards after turning on backwards GPIO pin no = ",BACKWARDS," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(BACKWARDS,OFF)
petrolLeft = petrolLeft - PETROL_USED_PER_MOVE
print ("Current driver is ",driversName)

#Change the driver
driversName = "Minnie"
print ("New driver is ",driversName)

#Move the car left - remember we have to move forwards as well
GPIO.output(LEFT,ON)
GPIO.output(FORWARDS,ON)
print ("Moving left after turning on left GPIO pin no = ",LEFT," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(LEFT,OFF)
GPIO.output(FORWARDS,OFF)
petrolLeft = petrolLeft - PETROL_USED_PER_MOVE
print ("Current driver is ",driversName)

#Move the car right - rememeber we have to move forwards as well
GPIO.output(RIGHT,ON)
GPIO.output(FORWARDS,ON)
print ("Moving right after turning on right GPIO pin no = ",RIGHT," Petrol Left = ",petrolLeft)
time.sleep(3)
GPIO.output(RIGHT,OFF)
GPIO.output(FORWARDS,OFF)
petrolLeft = petrolLeft - PETROL_USED_PER_MOVE
print ("Current driver is ",driversName)

print("Finished moving, petrol left = ", petrolLeft)
#remember to cleanup the GPIO pins
GPIO.cleanup()
```

The above code shows some comments which makes it a bit easier to understand. As with the other things we are learning, these become more important as things get more complicated.

You may also have notice that you do not have to put the # symbol at the start of the line. You can put it half way through the line. This will not mean the Raspberry Pi will ignore the whole line it will only ignore anything after the hash.

• Driving Lesson review

Variables:

How to use:

1. Put the name on the left and the value on the right and choose a suitable name – the same as constants. Remember no spaces, and also the convention is to not use all upper case for variables:

```petrolLeft = 100
```

2. Unlike constants at a different point in the program you can then assign a different value to the variable.

3. Also remember you can use the variable itself when you are changing it’s value.

```petrolLeft = petrolLeft - 20
```

Why do they help?

1. You can use them to store things that change that you will want to use in your program – you will see the real use in the next few Driving Lessons.

How to use:

1. Put a # symbol and then write any useful comment after it on the same line

```#reduce the petrol left by its current value less 20
petrolLeft = petrolLeft - 20
```

Why do they help?

1. They make the code easier to understand

• Gotcha’s

In any engineering things rarely work first time so don’t worry. Hopefully the list below will help. If not email us at info@pi-cars.com and leave a comment providing your code and what you tried to do, the solution will be obvious as soon as you see it!

Next -> Driving Lesson #4 If statements

Previous -> Driving Lesson #2 Pi-Cars and Python 