Lesson 3: Loops and the pen
Esta página precisa de um navegador recente (com suporte a SharedArrayBuffer). Por favor, atualize Chrome, Edge, Firefox ou Safari para a versão mais recente.
English
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 |
English
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.
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
Draw a square with sides of 500 mm. Put the pen down and use a for loop.
Clique em Executar para ver a saída aqui.
Dificuldade 2/5 · Moedas 140 + pequeno bônus na primeira conclusão hoje
Draw a triangle with 3 sides of 600 mm. Use a loop and turn LEFT. Hint: at each corner the robot turns 120 degrees.
Clique em Executar para ver a saída aqui.