Lesson 3: Loops and the pen
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
Draw with the pen
- The robot has a pen under it.
pen.move(DOWN)puts it on the floor; the robot then draws a line where it drives. pen.move(UP)lifts it again.pen.set_pen_color(RED)changes the colour (BLACK, RED, GREEN or BLUE).
Words for this lesson
| Word | Meaning |
|---|---|
| pen | draws a line on the floor under the robot |
| loop | code that runs again and again |
| range(4) | count 4 times: 0, 1, 2, 3 |
| indent | the spaces before a line inside the loop |
A square with a loop
from vexcode_vr import *
def main():
pen.move(DOWN)
for i in range(4):
drivetrain.drive_for(FORWARD, 400, MM)
drivetrain.turn_for(RIGHT, 90, DEGREES)
vr_thread(main)
- The two indented lines repeat 4 times: drive one side, turn one corner.
- Four corners of 90 degrees are 360 degrees, so the robot ends where it started.
Difficulty 1/5 · 100 coins + a small bonus on first completion today
Draw a square with sides of 500 mm. Put the pen down and use a for loop.
Click Run to see the output here.
Difficulty 2/5 · 140 coins + a small bonus on first completion today
Draw a triangle with 3 sides of 600 mm. Use a loop and turn LEFT. Hint: at each corner the robot turns 120 degrees.
Click Run to see the output here.