Night 22: Draw a garden
คุณเรียนจบคืนนี้แล้วด้วย Python หรือรหัสเทียม และนับรวมในทั้งสองภาษา คุณสามารถฝึกอีกครั้งได้ตามต้องการ
Tonight: draw a garden
- Welcome to week 4: your pet gets its own world!
- Tonight you draw a garden with rows of flowers.
- You will learn one thing: a loop inside a loop.
Words for tonight
| Word | Meaning |
|---|---|
| nested | one thing inside another thing |
| row | a line from left to right |
| inner / outer | the loop inside / the loop around it |
Build a row, then output it
- Each
OUTPUTstarts a new line. So build the row in a STRING first.
DECLARE Row : STRING
DECLARE Col : INTEGER
Row ← ""
FOR Col ← 1 TO 5
Row ← Row & "#"
NEXT Col
OUTPUT Row
A loop inside a loop
- The inner loop builds one row. The outer loop does it again for each row.
DECLARE RowNumber, Col : INTEGER
DECLARE Row : STRING
FOR RowNumber ← 1 TO 3
Row ← ""
FOR Col ← 1 TO 5
Row ← Row & "#"
NEXT Col
OUTPUT Row
NEXT RowNumber

Row ← ""is inside the outer loop: every row starts empty.
Each row can be different
- The inner loop can use the outer loop's counter.
RowNumber |
inner loop | row looks like |
|---|---|---|
| 1 | FOR Col ← 1 TO 1 |
* |
| 2 | FOR Col ← 1 TO 2 |
** |
| 3 | FOR Col ← 1 TO 3 |
*** |
- Now draw a garden for your pet!
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Draw a garden of 3 rows with 4 flowers # in each row. Use a loop inside a loop. Build each row in Row, then output it. The output is three lines of ####.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Read a size Size with INPUT. Draw a square of *: Size rows with Size stars each.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Draw a triangle with 4 rows: 1 star, then 2, then 3, then 4. The inner loop depends on the row.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 4/5 · 280 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Draw a 4 × 4 board. Where RowNumber + Col is even, add #. Otherwise add .. The first row is #.#..
คลิก Run เพื่อดูผลลัพธ์ที่นี่