Lesson 16: Count on patrol
تحتاج هذه الصفحة إلى متصفح حديث (يدعم SharedArrayBuffer). يرجى تحديث Chrome أو Edge أو Firefox أو Safari إلى أحدث إصدار.
English
The night guard
- At night a robot guard checks the hall. Red tiles mark doors that are open.
- The robot drives up the hall and counts what its down eye sees.
- The number of tiles is different on every map, so the robot must really count. A fixed answer works on one map only.
Words for this lesson
| Word | Meaning |
|---|---|
| patrol | go round a place to check that all is OK |
| count | find how many there are: 1, 2, 3 … |
| variable | a name that keeps a value, like count |
| text | letters and words; str(3) makes the text "3" |
English
Counting with a variable
| What happens | count |
|---|---|
| start | 0 |
first red tile: count = count + 1 |
1 |
| second red tile | 2 |
| third red tile | 3 |
| end: print "Red tiles: 3" | 3 |
count = count + 1means: take the old value, add 1, keep the new value.
English
Why drive off the tile?
| Without the 200 mm drive | With it |
|---|---|
| the robot moves 50 mm at a time, so it is on one 200 mm tile for 4 steps | it counts the tile, then drives past it |
| 3 tiles are counted as 12 | 3 tiles are counted as 3 |
from vexcode_vr import *
def main():
count = 0
for step in range(5):
count = count + 1
brain.print("Count: " + str(count))
brain.new_line()
vr_thread(main)
- The loop runs 5 times, so the last line shows
Count: 5. - In lesson 9 you solved the same problem with a variable
on_redthat remembers "I am on red now". Driving off the tile is another way. Both stop a tile from being counted twice.
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
The night guard robot checks the hall. Drive up until the wall is 100 mm away, 50 mm at a time, and count the red tiles on the floor. Each tile is 200 mm long, so after you count one, drive 200 mm to get off it. At the end print Red tiles: and the number. Every map has a different number of tiles.
اضغط تشغيل لرؤية المخرجات هنا.
الصعوبة 4/5 · 280 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Now there are red and blue tiles. Count both colours in two variables and print them like this: Red: 3, Blue: 2.
اضغط تشغيل لرؤية المخرجات هنا.