Validation
Python for IGCSE CS Lesson 7 2:23 English narration · English + 中文 subtitles burned in
Chapters
Transcript
Whatever you ask for, someone will type something else.
不管你要的是什么,总有人会输入别的东西。
An age of forty-five is fine.
年龄 45 没问题。
An age of nine hundred is not, and neither is a negative age — but the program cannot tell on its own, and it will happily calculate with either.
年龄 900 就不对,负数的年龄也不对—— 但程序自己分辨不出来,它会拿着这两个数照样算下去。
Validation means checking that a value is sensible before you use the value.
验证的意思,就是在使用这个值之前,先检查它是否合理。
Garbage in, garbage out is not a joke; it is a description of what happens.
"垃圾进,垃圾出"不是一句玩笑;它描述的就是实际会发生的事。
The syllabus names six checks, and you can be asked to choose between them.
考纲列出六种检查,而且可能会让你在它们之间做选择。
A range check keeps a number between two limits.
范围检查让数字保持在两个界限之间。
A length check counts characters.
长度检查数的是字符个数。
A presence check makes sure something was typed at all.
存在性检查确认用户确实输入了东西。
And there are three more you should know by name: a type check, a format check, and a check digit.
另外还有三种,名字你也该知道: 类型检查、格式检查,还有校验位。
You write the first three today, but the exam will ask you to pick the right one for a situation.
今天你写的是前三种, 但考试会让你为某个情境挑出正确的那一种。
Now a pair of words that sound similar and ask completely different questions.
现在来看一对听起来很像、问的却是完全不同问题的词。
Validation asks: is this value sensible?
验证(validation)问的是:这个值合理吗?
Verification asks: was it typed in correctly?
核对(verification)问的是:它有没有被正确地输入?
A double-entry check — typing a password twice — is verification.
让你把密码输入两遍的双重输入检查,属于核对。
And here is why both exist: a typo can produce a perfectly sensible value.
而这就是两者都要存在的原因: 一个打字错误完全可能产生一个合理的值。
Type 32 instead of 23 and every validation check in the world will accept it.
把 23 打成 32,世界上所有的合理性检查都会接受它。
The first task is a range check on an age, and there is one mistake to avoid.
第一道题是对年龄做范围检查,有一个错误要避免。
Write only one limit — at most a hundred and twenty — and minus three sails straight through, because it certainly is at most a hundred and twenty.
如果只写一个界限——不超过 120—— 那么 -3 会顺利通过,因为它确实不超过 120。
A range check needs both ends.
范围检查需要两端。
Python lets you write the whole thing as one chain, which reads exactly like the mathematics.
Python 允许你把整件事写成一条连写的比较, 读起来就和数学式一模一样。
Four things to take with you.
带走四点。
One: validation asks whether the value is sensible.
第一:验证问的是这个值是否合理。
Two: verification asks whether it was entered correctly.
第二:核对问的是它有没有被正确输入。
Three: the six checks are range, length, type, presence, format, check digit.
第三:六种检查是范围、长度、类型、存在性、格式、校验位。
Four: a range check needs a lower AND an upper limit.
第四:范围检查需要下限和上限两个。
Now write the three functions.
现在把那三个函数写出来。