习题 21: 函数可以返回东西

阅读原文

你已经学过使用 = 给变量命名,以及将变量定义为某个数字或者字符串。接下来我们 将让你见证更多奇迹。我们要演示给你的是如何使用 = 以及一个新的 Python 词汇 return 来将变量设置为“一个函数的值”。有一点你需要及其注意,不过我们暂且不讲, 先撰写下面的脚本吧:

def add(a, b):
    print "ADDING %d + %d" % (a, b)
    return a + b

def subtract(a, b):
    print "SUBTRACTING %d - %d" % (a, b)
    return a - b

def multiply(a, b):
    print "MULTIPLYING %d * %d" % (a, b)
    return a * b

def divide(a, b):
    print "DIVIDING %d / %d" % (a, b)
    return a / b


print "Let's do some math with just functions!"

age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)

print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)


# A puzzle for the extra credit, type it in anyway.
print "Here is a puzzle."

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

print "That becomes: ", what, "Can you do it by hand?"

现在我们创建了我们自己的加减乘除数学函数: add, subtract, multiply, 以及 divide。重要的是函数的最后一行,例如 add 的最后一行是 return a + b, 它实现的功能是这样的:

  1. 我们调用函数时使用了两个参数: ab
  2. 我们打印出这个函数的功能,这里就是计算加法(adding)
  3. 接下来我们告诉 Python 让它做某个回传的动作:我们将 a + b 的值返回(return)。 或者你可以这么说:“我将 ab 加起来,再把结果返回。”
  4. Python 将两个数字相加,然后当函数结束的时候,它就可以将 a + b 的结果赋予 一个变量。

和本书里的很多其他东西一样,你要慢慢消化这些内容,一步一步执行下去,追踪一下究竟 发生了什么。为了帮助你理解,本节的加分习题将让你解决一个迷题,并且让你学到点比较 酷的东西。

What You Should See

$ python ex21.py
Let's do some math with just functions!
ADDING 30 + 5
SUBTRACTING 78 - 4
MULTIPLYING 90 * 2
DIVIDING 100 / 2
Age: 35, Height: 74, Weight: 180, IQ: 50
Here is a puzzle.
DIVIDING 50 / 2
MULTIPLYING 180 * 25
SUBTRACTING 74 - 4500
ADDING 35 + -4426
That becomes:  -4391 Can you do it by hand?

Study Drills

  1. 如果你不是很确定 return 的功能,试着自己写几个函数出来,让它们返回一些值。 你可以将任何可以放在 = 右边的东西作为一个函数的返回值。
  2. 这个脚本的结尾是一个迷题。我将一个函数的返回值用作了另外一个函数的参数。我将 它们链接到了一起,就跟写数学等式一样。这样可能有些难读,不过运行一下你就知道结果 了。接下来,你需要试试看能不能用正常的方法实现和这个表达式一样的功能。
  3. 一旦你解决了这个迷题,试着修改一下函数里的某些部分,然后看会有什么样的结果。 你可以有目的地修改它,让它输出另外一个值。
  4. 最后,颠倒过来做一次。写一个简单的等式,使用一样的函数来计算它。

这个习题可能会让你有些头大,不过还是慢慢来,把它当做一个游戏,解决这样的迷题正是 编程的乐趣之一。后面你还会看到类似的小谜题。

Common Student Questions

Why does Python print the formula or the functions "backward"?
It's not really backward, it's "inside out." When you start breaking down the function into separate formulas and function calls you'll see how it works. Try to understand what I mean by "inside out" rather than "backward."
How can I use raw_input() to enter my own values?
Remember int(raw_input())? The problem with that is then you can't enter floating point, so also try using float(raw_input()) instead.
What do you mean by "write out a formula"?
Try 24 + 34 / 100 - 1023 as a start. Convert that to use the functions. Now come up with your own similar math equation and use variables so it's more like a formula.

Copyright (C) 2010 by
Author: Zed Shaw
Translator:Zander Wong

results matching ""

    No results matching ""