Skip to content

Making decisions

Python Basics Lesson 6 3:32 English narration · English + 中文 subtitles burned in

space play · ←/→ 5s · j/l 10s · f fullscreen · ,/. speed

Chapters

Transcript
Everything you have written so far does the same thing every time it runs. 到现在为止你写的程序,每次运行做的都是同一件事。
Real programs need to choose. 真正的程序需要做选择。
So you ask a question with a true-or-false answer — is this score at least fifty? 于是你提出一个答案只有真或假的问题——这个分数有没有达到 50?
If the answer is yes, the program takes one path and prints Pass. If it is no, it takes the other and prints Fail. 如果答案是有,程序走一条路,打印 Pass;如果没有,就走另一条路,打印 Fail。
One test, two paths, and exactly one of them happens. 一个判断,两条路,而且只会发生其中一条。
In Python that fork has a shape you must copy exactly. 在 Python 里,这个分叉有一个必须照抄的形状。
The line begins with if, then the condition, and it ends with a colon. 这一行以 if 开头,接着是条件,最后以冒号结束。
The next line is indented by four spaces, and that indented line is the part that runs only when the condition is true. 下一行缩进四个空格,缩进的这一行,就是只有条件为真时才执行的部分。
Anything back at the left margin is outside the if altogether — it runs whatever the answer was. 回到最左边的内容完全在 if 之外——不管答案是什么它都会执行。
Python has no ENDIF: the indentation IS the block. Python 没有 ENDIF:缩进本身就是代码块。
The condition is nearly always a comparison, and there are six. 条件几乎总是一个比较,一共有六种。
Equal, not equal, less than and greater than. 等于、不等于、小于、大于。
Then the two that include the boundary: less than or equal to, which you can read as at most, and greater than or equal to, at least. 还有两个把边界也算进去的:小于等于,可以读作“至多”;大于等于,也就是“至少”。
And now the single most common beginner bug in any language: double equals tests whether two things are the same; a single equals stores a value. 接下来是所有语言里最常见的初学者错误:双等号判断两个东西是否相同; 单等号是把一个值存起来。
Test with two, store with one. 判断用两个,赋值用一个。
With more than two outcomes you add elif — short for else if — and finish with else. 如果结果多于两种,就加上 elif——它是 else if 的缩写——最后用 else 收尾。
Python checks them from the top, in order, and stops the moment one is true. Python 从上往下按顺序检查,一旦有一个为真就停下。
Follow a score of seventy-two. 跟着 72 分走一遍。
The first test fails: seventy-two is not eighty or more. 第一个判断不成立:72 没有达到 80。
The second one is true, so it prints B. 第二个为真,于是打印 B。
And the else is never even looked at. 而 else 根本就没有被看过。
That order matters: put the widest test first and everything below it becomes unreachable. 顺序很重要:如果把范围最宽的判断放在最前面,它下面的都永远轮不到。
Sometimes one question is not enough. 有时候一个问题不够用。
And is true only when both sides are true. and 只有在两边都为真时才为真。
Or is true when at least one side is. or 只要至少有一边为真就为真。
And not simply flips the answer over. not 则是把答案反过来。
The teenager test in this lesson needs and, because a teenager is thirteen or more and nineteen or less — both must hold, and a value like twenty-five passes the first half while failing the whole thing. 这一课里判断青少年要用 and,因为青少年是 13 岁及以上,而且 19 岁及以下—— 两个条件都要成立;像 25 这样的值通过了前半个条件,整体却是假。
Now the lesson's first task. 现在做课程里的第一道题。
Read the score with input, wrapped in int as you learned two lessons ago. 用 input 读入分数,并像两课前学的那样用 int 包起来。
Then the test: if score is greater than or equal to fifty, colon. 然后是判断:if score 大于等于 50,冒号。
Underneath it, the indented print of Pass. 下面缩进一行,打印 Pass。
Then else, with its own colon, and its own indented print of Fail. 接着是 else,它也有自己的冒号, 以及自己缩进的一行,打印 Fail。
Give it seventy-two and the True arm runs. Give it forty-nine and the other arm runs — never both. 输入 72,走真的那条路; 输入 49,走另一条——永远不会两条都走。
Four things to take with you. 带走四点。
One: the if line ends with a colon, and the indented block under it is what the if controls. 第一:if 那一行以冒号结束,它下面缩进的代码块就是 if 控制的部分。
Two: double equals tests, single equals stores. 第二:双等号是判断,单等号是赋值。
Three: elif and else are checked in order, and the first true one wins. 第三:elif 和 else 按顺序检查,第一个为真的胜出。
Four: and needs both sides, or needs one, not flips it. 第四:and 要求两边都成立,or 只要一边,not 把结果反过来。
Now go and do the three tasks; the third one needs and. 现在去做那三道题;第三题需要用 and。

Log in or create account

IGCSE, A-Level & AP