Lesson 44: Follow the flowchart
此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
Read the flowchart, write the code
- A flowchart shows an algorithm with shapes and arrows.
- In the exam you often trace a flowchart or turn it into code.
| Shape | Meaning |
|---|---|
| rounded box | START or STOP |
| rectangle | a process: do something, set a variable |
| diamond | a decision: Yes or No |
| parallelogram | INPUT or OUTPUT |
English
Two kinds of loop

- A: test first (pre-condition loop, WHILE). The body may run 0 times.

- B: repeat first, test after (post-condition loop, REPEAT ... UNTIL). The body runs at least once.
English
In the exam
- 0478/22/F/M/24 Q8: trace a flowchart that finds the average size of groups of visitors. It inputs GroupSize until the rogue value 0; it counts the groups and totals the sizes, then outputs DIV(Total, NumberGroups).
- Try it with the exam data 7, 10, 2, 8, 3, 9, 0: NumberGroups ends at 6, Total at 39, and the output is "Average group size 6" (39 DIV 6 = 6). The 6 after the 0 is never read.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Turn flowchart A into Python. The diamond is tested 在...之前 the robot drives, so this is a while loop. Print Steps: and the number of steps. On the last map the box is already close: the robot must not move and must print Steps: 0.
点击“运行”查看此处输出。
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
现在 flowchart B. Here the robot drives 第一个 and tests after, so the body always runs at least once. Python has no REPEAT ... UNTIL, so use while True: and break when the test is true. Print Count: and the count. On the last map the robot starts on a green tile: it must still drive and count until the next green tile.
点击“运行”查看此处输出。