Lesson 9: Patterns
此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
A loop inside a loop
- Draw one shape, turn a little, and draw it again: a flower appears.
- The inner loop draws the shape. The outer loop repeats it.
Words for this lesson
| Word | Meaning |
|---|---|
| nested loop | a loop inside another loop |
| pattern | a shape repeated in a regular way |
| random | chosen by chance, different each run |
English
A flower of squares
import turtle
for petal in range(12):
for side in range(4):
turtle.forward(80)
turtle.left(90)
turtle.left(30)
English
Random art
- The
randommodule picks numbers and colours by chance. Run it twice!
import turtle
import random
turtle.penup()
for i in range(30):
turtle.goto(random.randint(-150, 150), random.randint(-150, 150))
turtle.dot(random.randint(10, 40), random.choice(["red", "gold", "teal", "purple"]))
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Draw a flower: 12 squares of size 80, turning left(30) after each square.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Draw a grid of dots: 4 rows and 5 columns, 40 steps apart. Use a loop inside a loop and goto(x, y).
点击“运行”查看此处输出。