World 5: While and lists
The last world
- In the last world your code looks at the map itself.
- A while loop walks until something is true. A list keeps many moves.
Words for this world
| Word | Meaning |
|---|---|
| while | repeat as long as something is true |
| list | many values in one variable |
| search | look at every place to find something |
Walk until you find it
road = "P..$"
pos = 0
while road[pos] != "$":
print("right")
pos = pos + 1
print("dig")
- The loop stops when the pet stands on the treasure.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The road is "P...$..H". Use a while loop: walk right while the square is not $. Dig, then walk right while the square is not H.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Pack the moves into a list first. Add right และ down ไป moves three times. Then print every move in the list.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
This route list has words the pet does not know (jump, sing). Print only the real moves: up, down, left, right.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 4/5 · 280 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Let the code find home on the map. Search every row and column for H, keep its position, then print the moves to get there.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Walk right while the next square is not a tree. Then go down and one step right home.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 5/5 · 400 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Final level! Let the code find both the treasure $ and home H on the map. Walk to the treasure, dig, then walk home. (Go right first, then down.)
คลิก Run เพื่อดูผลลัพธ์ที่นี่