Code for Life
Search…
Blockly Guide
Blockly - Python guide for Rapid Router

Van starting block

1
import van
2
v = van.Van()
Copied!
The starting code has been created ‘behind the scenes‘ in Python and has to be imported.
Those two lines are therefore automatically added to your new program in Rapid Router. When you open the Python pane, you should see this code. These two lines are required before all other commands.
After this code, the van object, which is the object controlling van movement on the map, is indicated by the variable v.
You could change the name of this object to whatever you like, but be consistent and use the same name with the commands afterwards.

Move forwards, turn left, turn right

1
v.move_forwards()
2
v.turn_left()
3
v.turn_right()
Copied!
move_forwards, turn_left and turn_right are, in fact, procedures. Once called, they cause the van to move. This is why when we want the van to move forwards, we call its move_forwards procedure by putting () after its name (e.g. v.move_ forwards())
Note the underscore (“_”) is an essential part of the name.

Repeat n times

1
for number in range(n):
Copied!
This code will repeat all commands indented underneath it n times, where n is a whole number such as 3. It works with any name inserted where the word number is shown in the example. (The built-in range function just tells the loop how many times to repeat.)
1
for count in range(3):
2
v.move_forwards()
Copied!
The example on the left would repeat v.move_forwards() 3 times. You can use the count variable inside the body of the loop if you want the van’s behaviour to differ depending on how many times the for loop was executed. On the first time through the loop, its value will be 0, incremented by one each time around the loop.
You must use a colon : at the end of the for line because this indicates that a sequence of instructions should follow (we call them the body of the loop). These instructions must all be indented by the same amount (ideally by 4 spaces).

Wait

1
v.wait()
Copied!
A procedure which when executed or ‘called’, causes the van to wait.

If, else if and else

1
if v.is_road(‘FORWARD’):
2
v.move_forwards()
3
elif v.is_road(‘LEFT’):
4
v.turn_left()
5
else:
6
v.turn_right()
Copied!
The indentation of the instructions inside a block must match. Ideally, they would be indented 4 spaces more than the previous statement.
if, elif and else statements must all be indented to the same level as each other, and all require a colon : at the end of their statement.
elif is short for else if
1
if v.at_traffic_light(‘RED’):
2
v.wait()
Copied!
The example to the left will cause the van to wait if it is currently at a red traffic light.

Conditions

1
v.is_road(‘FORWARD’)
2
v.is_road(‘LEFT’)
3
v.is_road(‘RIGHT’)
4
v.at_traffic_light(‘RED’)
5
v.at_traffic_light(‘GREEN’)
Copied!
These conditions can be added after an if, elif or else statement.
Each of the conditions are functions that check the state the van is in (i.e. what kind of road is ahead or what colour the traffic light is) and returns True or False.
If a variable can only be True or False, it is called a “Boolean”.

Repeat until at destination

1
while not v.at_destination():
2
v.move_forwards()
Copied!
The while not statement repeats until the condition is True.
Remember the colon denotes a set of instructions to be followed if the while condition is met (we call it the body of the loop).

Repeat while traffic light is red

1
while v.at_traffic_light(‘RED’):
2
v.wait()
Copied!
These instructions must be consistently indented, ideally by 4 spaces.
This example will cause the van to wait until the traffic lights are no longer red.

Procedures

1
def procedurename():
Copied!
To create a procedure, you use the def keyword. The procedure needs a meaningful name where procedurename is placed in the example. You must have a pair of brackets () and a colon : after it.
1
def proc1():
2
v.turn_left()
3
v.turn_right()
4
5
proc1()
Copied!
All subsequent statements that are to be part of the procedure must be indented to the same level as each other (ideally 4 spaces).
The procedure is then executed (or called), by typing the name of the procedure followed by a pair of brackets ().
In this example, the proc1 procedure will move the van left and then right when called. In reality, it is better to choose a more meaningful name for your procedure.
1
def forward_left(n):
2
for count in range(n):
3
v.move_forwards()
4
v.turn_left()
5
6
forward_left(4)
Copied!
What happens inside the procedure can be changed each time by passing in arguments. Arguments available to the procedure are defined in between the two brackets, such as the argument n in this example. This value is then used to change how many times the loop is executed.
When calling a procedure with an argument, you must define the value for that argument when you call the procedure. In this case, we are calling the forward_left procedure with the argument value of 4, which means the loop will execute 4 times (the van will move forward 4 times before turning left).

Last modified 4mo ago