Planning & pseudocode
Python Basics Lesson 13 3:23 English narration · English + 中文 subtitles burned in
Chapters
Transcript
When a task gets hard, the temptation is to start typing and hope.
任务一变难,人就忍不住直接开敲,边写边指望它能行。
The faster way is to write the steps down in ordinary sentences first.
更快的办法是先用普通的句子把步骤写下来。
Read the number.
读入这个数。
Check whether it divides by two.
看看它能不能被 2 整除。
Say even or odd.
说出是偶数还是奇数。
Output the answer.
输出答案。
Four sentences, and anybody — your teacher, a classmate, you tomorrow — can check the thinking before a single line of code exists.
四句话,而任何人——你的老师、同学,或者明天的你—— 都能在还没有一行代码的时候检查这个思路。
Two habits carry most of the weight.
有两个习惯几乎撑起了全部。
The first is decomposition: split one big job into smaller problems that each fit in your head.
第一个是分解: 把一件大事拆成若干个小问题,每一个都能装进你的脑子里。
Report on a class becomes read the scores, average them, count the passes — and each of those is a function you already know how to write.
“给一个班的成绩做报告”变成:读入成绩、求平均、数出及格人数—— 而这几件事,每一件都是你已经会写的函数。
The second is abstraction: deciding what to leave out.
第二个是抽象:决定把什么略去。
A map shows the roads and not every tree, and it is more useful for exactly that reason.
地图只画道路而不画每一棵树, 它有用,恰恰就是因为这一点。
A flowchart is the same plan drawn instead of written, and it uses two shapes you must recognise in an exam.
流程图是同一个计划,只不过是画出来而不是写出来的, 它用到两个你在考试里必须认得的形状。
A rectangle is a step: do this.
矩形是一个步骤:做这件事。
A diamond is a decision with a yes and a no leaving it.
菱形是一个判断,从它引出“是”和“否”两条线。
Follow it down from the start: read n, then reach the test.
从起点往下走:读入 n,然后到达判断。
From there the path splits one way or the other, and the two branches say what happens in each case.
从那里路径分成两条,两个分支分别说明各自会发生什么。
Notice there is no third option — a decision has exactly two exits.
注意没有第三种可能——一个判断正好有两个出口。
Everything you have learned so far fits into three shapes.
你到目前为止学的所有东西,都能装进三种结构里。
Sequence: steps run one after another, in the order written.
顺序:各个步骤按写下的顺序一个接一个地执行。
Selection: an if chooses a path.
选择:if 选择一条路径。
Iteration: a for or a while repeats them.
循环:for 或 while 把它们重复执行。
And that is nothing else — every program in this course, and every program in your exam, is built from those three, nested inside one another.
除此之外就没有别的了——这门课里的每一个程序,你考试里的每一个程序, 都是由这三种结构互相嵌套搭起来的。
Cambridge writes plans in pseudocode, which looks like code but never runs.
剑桥考试用伪代码来写计划,它看起来像代码,但从来不需要真的运行。
A left arrow means store — it is your equals sign.
左箭头表示“存入”——就是你的等号。
FOR and NEXT are the counting loop, and OUTPUT is print.
FOR 和 NEXT 是计数循环,OUTPUT 就是 print。
Put the Python beside it and the two match line for line, with only the spelling different.
把 Python 放在旁边,两边一行对一行,只是写法不同而已。
That is the point: if you can write the pseudocode, the Python is a translation, not a puzzle.
这正是关键:只要你能写出伪代码,Python 就只是翻译,而不是难题。
Now the lesson's first task, done the way this lesson asks.
现在用这一课的方法来做课程里的第一道题。
Start with the plan: four sentences, no Python at all.
先写计划:四句话,完全没有 Python。
Then translate.
然后翻译。
Each sentence becomes one construct — read becomes int of input, the remainder question becomes if n percent two equals equals zero, and the two answers become the two branches.
每一句话变成一个结构——“读入”变成 int(input()), “有没有余数”变成 if n % 2 == 0,两种答案变成两个分支。
When the plan is right, this step is mechanical.
只要计划是对的,这一步就是机械的。
Four things to take with you.
带走四点。
One: an algorithm is the steps, and you write them before the code.
第一:算法就是那些步骤,而且要在写代码之前写下来。
Two: decompose — split a big problem into small ones you can each solve.
第二:分解——把一个大问题拆成一个个你能各自解决的小问题。
Three: abstract — keep only the details that matter.
第三:抽象——只保留真正重要的细节。
Four: sequence, selection and iteration, and that is all there is.
第四:顺序、选择、循环,就这些。
Now do the three tasks; each one hands you a plan in words to turn into Python.
现在去做那三道题;每一题都给了你一段用文字写的计划,把它翻译成 Python。