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.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 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).
คลิก Run เพื่อดูผลลัพธ์ที่นี่