Hàm số
Python Cơ bản Bài học 10 2:53 Thuyết minh tiếng Anh · Có thể chuyển đổi phụ đề tiếng Anh và tiếng Trung
Chương
Biên bản ghi âm
You have written the same four lines twice: total starts at zero, loop, divide by the length.
同样的四行你已经写了两遍:total 从 0 开始,循环,再除以长度。
If a third list turns up you will copy it again — and the day you fix a bug in one copy, the others keep the bug.
如果再来第三个列表,你还得再复制一遍——而当你在其中一份里改掉一个错误时, 其他几份还带着这个错误。
Instead, give that job a name.
换个做法:给这件事取个名字。
Write it once, and from then on the whole calculation is a single word with the data in brackets.
只写一次,从此整个计算就是一个词,把数据放在括号里。
The definition line has three pieces.
定义这一行有三个部分。
First the word def, which tells Python you are defining something, not running it — the body does nothing until somebody calls it.
首先是 def,它告诉 Python 你在定义一个东西,而不是在运行它—— 在有人调用它之前,函数体什么也不做。
Then the name you will call it by, chosen the same way you choose a variable name.
接着是你以后调用它时用的名字,取名的规矩和取变量名一样。
Then, in the brackets, the parameters: names for the values it will be given.
然后是括号里的形参:将来传进来的那些值的名字。
A colon ends the line, and the body is indented under it, exactly like an if or a for.
冒号结束这一行,函数体缩进在它下面,和 if、for 完全一样。
Picture a function as a little machine.
把函数想象成一台小机器。
To use it, you hand it two values — two and three — and those arrive inside as a and b.
要用它,你递给它两个值——2 和 3—— 它们在里面就变成了 a 和 b。
The body runs.
函数体开始执行。
Then return hands one value back out: five.
然后 return 把一个值交还出来:5。
That is the whole shape, and the last step is the one beginners skip: you must catch it in a variable, or use it straight away, because the value goes back to wherever the call was written.
整个形状就是这样, 而最后一步正是初学者容易跳过的:你必须把它接到一个变量里,或者立刻用掉, 因为这个值是回到调用它的那个地方的。
These two look almost the same and are completely different.
这两段看起来几乎一样,实际上完全不同。
Print shows it on the screen and keeps nothing: the function ends having produced no value, so x is None and multiplying it crashes.
print 把结果显示在屏幕上,什么也没留下:函数结束时没有产生任何值, 所以 x 是 None,再拿它去做乘法就会报错。
Return hands it back: x is five, and five times two prints ten.
return 则把结果交还回来:x 是 5,5 乘以 2 打印出 10。
A useful test — if you cannot store the answer in a variable, the function returned nothing.
有个好用的判断办法——如果你没法把答案存进一个变量,那这个函数就什么都没返回。
Now the lesson's third task: return the larger of two values.
现在做课程里的第三道题:返回两个值中较大的那个。
Inside the function you already know how to compare them — an if with a greater-than.
在函数里面,比较大小你已经会了——一个 if 加上大于号。
Return a when a wins, otherwise return b.
a 大就 return a,否则 return b。
Test it either way round: three and nine gives nine, nine and three also gives nine.
两个方向都测一下: 3 和 9 得到 9,9 和 3 也得到 9。
And when the two are equal, either branch returns the same number, so four and four gives four.
两个数相等时, 走哪一条分支返回的都是同一个数,所以 4 和 4 得到 4。
Notice return leaves the function immediately.
注意 return 会立刻离开这个函数。
Four things to take with you.
带走四点。
One: def, a name, brackets, a colon — then an indented body.
第一:def、名字、括号、冒号——然后是缩进的函数体。
Two: parameters are names for the values you pass in.
第二:形参是你传进去的那些值的名字。
Three: return hands one value back to whoever called the function.
第三:return 把一个值交还给调用这个函数的地方。
Four: print shows a value to a human; only return gives a value to the rest of your program.
第四:print 是给人看的;只有 return 才能把值交给你程序的其余部分。
Now do the three tasks — all three return, and none of them print.
现在去做那三道题——三题都要 return,没有一题需要 print。
Bình luận
Chưa có bình luận nào. Hãy là người đầu tiên đặt câu hỏi hoặc chia sẻ điều gì đó.