Lesson 3: Loops and the pen
تحتاج هذه الصفحة إلى متصفح حديث (يدعم SharedArrayBuffer). يرجى تحديث Chrome أو Edge أو Firefox أو Safari إلى أحدث إصدار.
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.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Draw a square with sides of 500 mm. Put the pen down and use a for loop.
اضغط تشغيل لرؤية المخرجات هنا.
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Draw a triangle with 3 sides of 600 mm. Use a loop and turn LEFT. Hint: at each corner the robot turns 120 degrees.
اضغط تشغيل لرؤية المخرجات هنا.