新手学python-笨方法学python-ex21-函数可以返回东西

跟着笨方法学python个教程学习,边学边做习题边记录。

新手学python-笨方法学python-ex21-函数可以返回东西 - 学习记录

课程内容:

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

def add(a, b):
    print "ADDIND %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, Herght: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)

# A puzzle for the extra credit, type it in anyway.

print "Here's 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.我们调用函数时使用了两个参数: a 和 b 。
2.我们打印出这个函数的功能,这里就是计算加法(adding)
3.接下来我们告诉 Python 让它做某个回传的动作:我们将 a + b 的值返回(return)。或者你可以这么说:“我将 a 和 b 加起来,再把结果返回。”
4.Python 将两个数字相加,然后当函数结束的时候,它就可以将 a + b 的结果赋予一个变量。
和本书里的很多其他东西一样,你要慢慢消化这些内容,一步一步执行下去,追踪一下究竟发生了什么。为了帮助你理解,本节的加分习题将让你解决一个迷题,并且让你学到点比较酷的东西。


运行该程序:


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

$


加分习题:
1.如果你不是很确定 return 的功能,试着自己写几个函数出来,让它们返回一些值。你可以将任何可以放在 = 右边的东西作为一个函数的返回值。

 

def larger_than(a, b):
    print "If %d is larger than %d?" % (a, b)
    return a > b

def larger_or_equal(a, b):
    print "If %d is larger or equal than %d?" % (a, b)
    return a >= b

def newsum(a, b):
    print "The sum of %d and %d is %d" %(a, b, a+b)
    return a + b
    
no = larger_than(1, 2)
print no
yes = larger_or_equal(2, 1)
print yes
sum = newsum(1, 2)

sums = newsum(newsum(1,2), newsum(3,4))


运行该程序:

$python ex21-1.py
If 1 is larger than 2?
False
If 2 is larger or equal than 1?
True
The sum of 1 and 2 is 3
The sum of 1 and 2 is 3
The sum of 3 and 4 is 7
The sum of 3 and 7 is 10

$


2.这个脚本的结尾是一个迷题。我将一个函数的返回值用作了另外一个函数的参数。我将它们链接到了一起,就跟写数学等式一样。这样可能有些难读,不过运行一下你就知道结果了。接下来,你需要试试看能不能用正常的方法实现和这个表达式一样的功能。


def add(a, b):
    print "ADDIND %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)
print "+++++++++++++++++++++++++++++++++"
height = subtract(78, 4) 
print "+++++++++++++++++++++++++++++++++"   
weight = multiply(90, 2)
print "+++++++++++++++++++++++++++++++++"
iq = divide(100, 2)
print "+++++++++++++++++++++++++++++++++"

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

# A puzzle for the extra credit, type it in anyway.

print "Here's is a puzzle."

#what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
print "*********************************"
x1 = divide(iq, 2)
print "*********************************"
x2 = multiply(weight, x1)
print "*********************************"
x3 = subtract(height, x2)
print "*********************************"
what = add(age, x3)
print "+++++++++++++++++++++++++++++++++"
print "x1: %d, x2: %d, x3: %d" % (x1, x2, x3)
print "+++++++++++++++++++++++++++++++++"
print "That becomes: ", what, "Can you do it by hand?"

 


运行该程序:

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

$

 

3.一旦你解决了这个迷题,试着修改一下函数里的某些部分,然后看会有什么样的结果。你可以有目的地修改它,让它输出另外一个值。

4.最后,颠倒过来做一次。写一个简单的等式,使用一样的函数来计算它。

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

 

至此,-ex21-函数可以返回东西这一小节的学习结束了。

Saturday, November 07, 2015 | Python

文章评论

No comments posted yet.

发表评论

Please add 3 and 1 and type the answer here: