Lesson 5: Functions
此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
Your own commands
- A function is a name for some lines of code.
def square(size):makes a new command calledsquare. sizeis a parameter: a value you give the function each time you call it.main()is a function too. You have used it from the start.
Words for this lesson
| Word | Meaning |
|---|---|
| function | a named group of commands |
| def | define (make) a function |
| parameter | a value the function gets, like size |
| call | run a function: square(300) |
English
Make a command, use it twice
from vexcode_vr import *
def square(size):
for i in range(4):
drivetrain.drive_for(FORWARD, size, MM)
drivetrain.turn_for(RIGHT, 90, DEGREES)
def main():
pen.move(DOWN)
square(200)
square(400)
vr_thread(main)
square(200)draws a small square,square(400)a big one. The code inside is written once.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Finish the function square(size), then call it twice in main() to draw a square of 300 mm and a square of 600 mm.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Write a function hexagon() that draws a shape with 6 sides of 400 mm, and call it. Turn LEFT 60 degrees at each corner.
点击“运行”查看此处输出。