Night 22: Draw a garden
你已使用 Python 或伪代码完成过这个夜间练习,两种语言均计入完成记录。你可以再次练习。
此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
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 |
English
Stay on the same line
printnormally starts a new line at the end.end=""tells it not to.print()alone just ends the line.
print("#", end="")
print("#", end="")
print("#", end="")
print()
print("next line")
English
A loop inside a loop
- The inner loop draws one row. The outer loop does it again for each row.
for row in range(3):
for col in range(5):
print("#", end="")
print()

- Count the indents:
print("#", end="")is inside both loops.print()is only inside the outer one.
English
Each row can be different
- The inner loop can use the outer loop's variable.
for row in range(1, 4):
for col in range(row):
print("*", end="")
print()
row |
inner loop runs | row looks like |
|---|---|---|
| 1 | 1 time | * |
| 2 | 2 times | ** |
| 3 | 3 times | *** |
- 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. The output is three lines of ####.
点击“运行”查看此处输出。
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Read a size n with int(input()). Draw a square of *: n rows with n stars each.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Draw a triangle with 4 rows: 1 star, then 2, then 3, then 4. The inner loop depends on the row.
点击“运行”查看此处输出。
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
Draw a 4 × 4 board. Where row + col is even, print #. Otherwise print .. The first row is #.#..
点击“运行”查看此处输出。