Documentation > Python
If Statement
What will you learn?
You will Learn how you can make your program react in different ways based on conditions. You will learn how to use the Python if statement.
What will your program do?
It will link two traffic lights to a track switch tile.
The traffic lights will change in function of the track switch position.
What will you need?
Required: nControl 2018.0 or higher.
Optional: a track switch, a track switch motor, 2 train traffic lights and the appropriate controllers to drive them. Without the hardware you can still simulate the behavior on screen.
Introduction
In this lesson we'll learn how make programs that respond in function specific conditions using the if statement. The goal is to create a program for a track switch tile that controls two train traffic lights. The traffic lights will automatically adjust in function of the position of the track switch.
If Statement
The if statement enables you to execute different operations in function of a specific condition. For example, you can execute different actions when the track switch is put to the 'straight' position then when it's put to the 'turn' position.
In Python the if statement has the following syntax:
if condition:
   executed when the condition is true
else:
   executed when the condition is false
There are two blocks of code, one to be execute when the condition is met and one for when the condition is not met. These blocks of code have to be indented, meaning that you have to add spaces in front of them so the code is moved to the right. Python is very strict with indentation because it's an integral part of the language syntax: it determines the end of the conditional code. Consider the following program:
if condition:
   ...
else:
   executed when the condition is false
   executed when the condition is false
   executed when the condition is false
always executed
The first three lines that follow the else keyword are executed with the condition is not met. The forth line is always executed because it's not part of the else code block as it's not indented. The indentation thus defined the end of the conditional code.
We you start programming, indentation might seem cumbersome, especially with Python as it's very strict when it comes to indentation. However, maintaining a proper indentation is fundamental in keeping your code readable. The fact that Python forces you to use indentation makes it an excellent language for beginners, as it creates the habit of properly indenting you code. There is no fixed number of spaces you should use to indent your code. Most programmers use 2, 3 or 4 spaces; the nControl examples are all indented with 3 spaces.
To conclude, note that the else section is optional. The following code is perfectly fine:
if condition:
   executed when the condition is true
always executed
Track Layout
Start by downloading the project below, it contains the track layout and the tiles that we will need for this lesson.

Let's open the project:

The project contains a small layout with a motorized switch and two train traffic lights. There's no custom code so these components don't interact. You can verify that by going to Simulation Mode and clicking the tiles:
Let's reprogram the tiles to link the switch and the two traffic lights.
The Track Switch
Open the code editor of the track switch tile to customize it's behavior:

The first thing our script has to do is making sure the track switch is flipped. This is done with the flipSwitch function of our track switch tile. Our first line of code thus becomes:
self.flipSwitch()
Next, we need to modify the lights. In order to access the traffic light tiles, we have to create variables containing the corresponding objects. Note that the traffic lights tiles are called 'Turn' and 'Straight'. The tile variables can be created with the following code:
light1 = self.getTile('Turn')
light2 = self.getTile('Straight')
We have to set the traffic lights in function of the position of track switch: if the track is in the 'straight' position the traffic light 1 has to be red while the traffic light 2 has be green. First we need to know the position of the track switch. In the documentation of the track switch tile you can see that this can be retrieved with the getState() function. Let's store the result in a variable called state, on in Python code:
state = self.getState()
Now we can use the if to change the traffic lights in function of the switch position:
if state == nConst.ST_STRAIGHT:
   light1.setRed()
   light2.setGreen()
else:
   light1.setGreen()
   light2.setRed()
Let's analyze this block of code. The first line specifies the condition; it compare the value of the variable state with the constant nConst.ST_STRAIGHT. So when state equals nConst.ST_STRAIGHT the switch is in the 'straight' position and line 2 and 3 are executed. This means that when the switch is put to the 'straight' position light 1 will turn red and light 2 will turn green. However, when the switch it put in the 'turn' position state will not equal nConst.ST_STRAIGHT and lines 5 and 6 are executed: light 1 will turn green and light 2 will turn red.
Note that two equal signs are used in the condition: state == nConst.ST_STRAIGHT. In Python == is the comparison operator, it compare two values. Single equal signs assign a value to a variable. So,
A == B
compares the values of A and B, while
A = B
assigns the value of B to A.
Using only one equal sign in a condition is a typical beginner's mistake. If you use A = B as condition it will always be met because it's not comparing the values, it's assigning the value of B to A. So if your else code block is not executed even when your condition is not met, check your code and make sure there are two equals signs in your condition!
Testing Your Program
To test your program: close the code editor, go so Simulation Mode and click the track switch tile. The traffic lights will now be adjusted when the switch position is changed.

Documentation > Python