Night 24: Move around
你已使用 Python 或伪代码完成过这个夜间练习,两种语言均计入完成记录。你可以再次练习。
此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
Tonight: move around
- Your pet has a map. Tonight it walks around on it!
- Nothing new to learn: you use
input(),whileandifon a map.
Words for tonight
| Word | Meaning |
|---|---|
| move | go from one place to another |
| position | where something is: x and y |
| command | a word that tells the program what to do |
English
Where is the pet?
- Two numbers say where the pet is:
x(left–right) andy(up–down). - On a screen,
y = 0is the top row. So "up" makesysmaller.
| Move | Change |
|---|---|
| up | y = y - 1 |
| down | y = y + 1 |
| left | x = x - 1 |
| right | x = x + 1 |

English
The game loop
- Read a command, change the world, repeat. Every game works like this.
x = 2
y = 2
moves = ["up", "left", "left"]
for move in moves:
if move == "up":
y = y - 1
elif move == "left":
x = x - 1
print("Now at", x, y)
- In the tasks, the moves come from
input()until the player typesstop.
English
Stay on the map
- Check the edge before you move.
x = 0
move = "left"
if move == "left" and x > 0:
x = x - 1
print(x)
xstays 0: the pet is already at the left wall.- Now take your pet for a walk!
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Your pet is at x = 2, y = 2. Read one move: up, down, left 或 right. Change x 或 y, then print them. left gives 1 2. up makes y smaller.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Now read moves again and again, until the move is stop. Change x and y for each move. At the end, print x and y.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
The map is 5 × 5: x and y go from 0 到 4. Do not let the pet walk off the map. A move into the wall does nothing.
点击“运行”查看此处输出。
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
After the moves, draw the 5 × 5 map: P where the pet is, . everywhere else. Row y and column x hold the P.
点击“运行”查看此处输出。