Blockly - Python guide for Rapid Router
from van import Van
my_van = Van()
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
You could change the name of this object to whatever you like, but be consistent and use the same name with the commands afterwards.
turn_rightare, 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_forwardsprocedure by putting () after its name (e.g.
Note the underscore (“_”) is an essential part of the name.
for number in range(n):
This code will repeat all commands indented underneath it
nis 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.)
for count in range(3):
The example on the left would repeat
my_van.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
forline 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).
A procedure which when executed or ‘called’, causes the van to wait.
The indentation of the instructions inside a block must match. Ideally, they would be indented 4 spaces more than the previous statement.
elsestatements must all be indented to the same level as each other, and all require a colon
:at the end of their statement.
elifis short for
The example to the left will cause the van to wait if it is currently at a red traffic light.
These conditions can be added after an
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
If a variable can only be True or False, it is called a “Boolean”.
while not my_van.at_destination():
whilenot statement repeats until the condition is
Remember the colon denotes a set of instructions to be followed if the
whilecondition is met (we call it the body of the loop).
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.
To create a procedure, you use the
defkeyword. The procedure needs a meaningful name where
procedurenameis placed in the example. You must have a pair of brackets () and a colon
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.
for count in range(n):
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).