Getting input
Python Basics Lesson 4 2:57 English narration · English + 中文 subtitles burned in
Chapters
Transcript
Every program so far has known everything in advance.
到目前为止,每个程序都事先知道所有内容。
Here the greeting works perfectly — for Sam.
这个问候程序运行得很好——对 Sam 来说。
You wrote the name into the program, so Sam is the only person it will ever greet.
你把名字写死在程序里,所以它永远只会问候 Sam。
The alternative is to ask at the moment the program runs.
另一种做法是:在程序运行的那一刻去问。
One word changes it, and now the same six characters of code greet anybody who sits down at the keyboard.
改一个词就够了,同样的几个字符,现在能问候任何一个坐到键盘前的人。
That word is input, with empty brackets after it.
那个词是 input,后面跟一对空括号。
When Python reaches it, the program simply waits — nothing else happens until somebody types a line and presses Enter.
当 Python 执行到它时,程序就停下来等待—— 在有人输入一行并按下回车之前,什么都不会发生。
Whatever they typed is then handed back, and here it is stored in the variable name.
他们输入的内容会被交回来,这里被存进变量 name。
The next line joins the two pieces of text with a plus and prints the result: Welcome, Mia.
下一行用加号把两段文字拼接起来并打印结果:Welcome, Mia。
Now the trap, and it catches everyone.
现在说那个陷阱,几乎每个人都会中招。
Whatever the user types comes back as a string — as text.
用户输入的任何东西,回来的都是字符串——文字。
Type the digit five and you do not get the number five, you get the text five.
你输入数字 5,得到的不是数字 5,而是文字 5。
And a plus between two strings does not add: it joins them.
而两个字符串之间的加号并不是相加:它是拼接。
So text five plus text five is text five-five, which reads as fifty-five and is really just two characters side by side.
所以文字 5 加文字 5 得到的是 55,看起来像五十五,其实只是两个字符并排放着。
With real numbers, the same plus gives ten.
如果是真正的数字,同样的加号给出的是 10。
The fix is one step, and it belongs at the moment you read.
解决办法只有一步,而且要放在读取的那一刻。
Follow the value across: somebody types seven, input hands back text, and if you stop there any maths goes wrong.
跟着这个值看过去: 有人输入 7,input 交回来的是文字,如果就此打住,之后的运算都会出错。
So wrap it in int, which turns text into a whole number — or float, if you want decimals.
所以用 int 把它包起来,把文字变成整数——如果需要小数,就用 float。
Only now can you add one and get eight.
到这时才可以加 1,得到 8。
Convert first, compute second: that order is the whole lesson.
先转换,后计算:这个顺序就是这一课的全部。
Here is the lesson's second task.
这是课程里的第二道题。
Read a whole number, then print it plus a hundred.
读入一个整数,然后打印它加上 100 的结果。
One line reads and converts: input, wrapped in int, stored in n.
一行完成读取和转换:input 外面包上 int,结果存进 n。
The second line does the sum inside the print.
第二行在 print 里面做加法。
Type seven and you get a hundred and seven.
输入 7,你得到 107。
Forget the conversion and the answer becomes seven thousand one hundred — the digit seven with the characters one, zero, zero stuck on the end.
如果忘了转换,答案会变成 7100——数字 7 后面接上字符 1、0、0。
That output is the clue: you joined text instead of adding.
那个输出就是线索:你拼接了文字,而不是做加法。
Four things to take with you.
带走四点。
One: input waits for a line and hands back what was typed.
第一:input 等待一行输入,并把输入的内容交回来。
Two: it is always a string, whatever it looks like.
第二:不管看起来像什么,它总是字符串。
Three: convert with int or float before any arithmetic.
第三:做任何算术之前,先用 int 或 float 转换。
Four: plus joins text but adds numbers — the same symbol, two jobs, decided by what is on either side.
第四:加号对文字是拼接,对数字是相加——同一个符号,两种作用, 取决于两边是什么。
Now go and do the three tasks; each one needs exactly one input.
现在去做那三道题;每题正好只需要一次 input。