Selection
Python for IGCSE CS Lesson 3 2:10 English narration · English + 中文 subtitles burned in
Chapters
Transcript
Selection is the second big idea in the syllabus: choosing what to do, based on a test.
选择是考纲里的第二个大概念:根据一个判断来决定做什么。
The test gives back true or false, and the program goes one way if it is true, and the other if it is false.
这个判断给出 true 或 false, 条件为真程序走一条路,为假就走另一条。
Exactly one of the two branches runs — never both, and with an else attached, never neither.
两个分支里恰好有一个会执行—— 绝不会两个都跑,而只要写了 else,也绝不会一个都不跑。
With more than two outcomes you use elif, and there is one thing about it that people get wrong.
当结果多于两种时,你会用 elif,而关于它有一点人们常常弄错。
Take a mark of sixty-five.
假设分数是 65。
The first test, seventy or more, is false.
第一个判断,是否 ≥70,为假。
The second, fifty or more — this one is true, so B is printed.
第二个,是否 ≥50——这个为真,于是打印出 B。
And then the chain stops.
然后这条链就停了。
The third rung is never even tested.
第三级根本没有被检查过。
Which is why the order of your tests decides the answer, not just the tests themselves.
这就是为什么决定答案的是判断的顺序,而不只是判断本身。
Two symbols look different on the exam paper from the ones you type.
有两个符号,考卷上的写法和你敲的不一样。
The exam writes equal-to as a single equals sign; Python needs a double one.
考卷把"等于"写成一个等号;Python 需要两个。
The exam writes not-equal-to as angle brackets; Python writes an exclamation mark and an equals.
考卷把"不等于"写成一对尖括号;Python 写成叹号加等号。
The rest are the same.
其余的都相同。
And the reason for the doubling matters: in Python a single equals assigns a value, so writing it inside a condition is an error, not a comparison.
而加倍的原因是有意义的: 在 Python 里,单个等号是赋值, 所以把它写在条件里是一个错误,而不是一次比较。
Here is the lesson's second task in both notations, so you can read across.
这是这一课的第二道题,两种写法并排放着,你可以横着读。
Same structure, same order, different punctuation.
同样的结构,同样的顺序,只是标点不同。
And notice the order once more: the highest test comes first.
再注意一次顺序:最高的判断写在最前面。
Put the fifty test on top and every mark above seventy would come out as a B — the program would still run, and every strong student would be marked down.
如果把 ≥50 那条放在最上面, 所有 70 分以上的成绩都会变成 B—— 程序照样能跑,而每一个优秀的学生都被压低了成绩。
Four things to take with you.
带走四点。
One: selection chooses a path from a true-or-false test.
第一:选择结构根据一个真假判断来选路。
Two: an elif chain stops at the first true test.
第二:elif 链在第一个为真的判断处停下。
Three: compare with a double equals, and assign with a single one.
第三:比较用双等号,赋值用单等号。
Four: the exam writes those as = and angle brackets.
第四:考卷把这两个写成 = 和一对尖括号。
Now do the three tasks.
现在去做那三道题。