Night 25: Find the treasure
คุณเรียนจบคืนนี้แล้วด้วย Python หรือรหัสเทียม และนับรวมในทั้งสองภาษา คุณสามารถฝึกอีกครั้งได้ตามต้องการ
Tonight: find the treasure
- Treasure is buried somewhere on the map! Your pet digs to find it.
- Nothing new to learn: you use
while,if,andand a flag together.
Words for tonight
| Word | Meaning |
|---|---|
| treasure | gold and other things of great value |
| dig | make a hole in the ground |
| distance | how far it is from one place to another |
Both must be right
- The treasure is at
x = 3,y = 1. A dig is right only when both match.
x = 3
y = 4
if x == 3 and y == 1:
print("Treasure!")
else:
print("Nothing here")

A flag stops the loop
- A flag is a True/False variable. It remembers if something happened.
found = False
digs = 0
while not found:
digs = digs + 1
if digs == 3:
found = True
print("Found after", digs, "digs")
not foundisTruewhilefoundisFalse.
The order of the hints
- Check
xfirst, theny. When you testy, you already knowxis right.
| Test | Hint |
|---|---|
x < 3 |
Go right |
x > 3 |
Go left |
y < 1 |
Go down |
| otherwise | Go up |
- Now help your pet find the treasure!
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Treasure is buried at x = 3, y = 1. Read where the pet digs: x, then y, each with int(input()). Print Treasure! หรือ Nothing here.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Help the pet! After a wrong dig, print one hint. If x is wrong, print Go right (x is too small) or Go left. If only y is wrong, print Go down (y too small) or Go up.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Let the pet dig again and again. Read x และ y for each dig. Print Nothing here after each miss. When it finds the treasure, print Treasure! Found in and the number of digs, then digs.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 4/5 · 280 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A better hint: how many steps away is the treasure? Read x และ y. If it is the treasure, print Treasure!. Otherwise print the steps, like 3 steps away. Steps are the x-distance plus the y-distance.
คลิก Run เพื่อดูผลลัพธ์ที่นี่