习题21
首先贴出自己照着敲出来的代码:
#-*- coding: utf-8 -*-
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?"
这一个习题主要是练习“return”的用法,通过加减乘除四个函数强化对返回值的理解。
—————————————————————————————————————————————————————————
加分习题1: 如果你不是很确定 return 的功能,试着自己写几个函数出来,让它们返回一些值。你可以将任何可以放在 = 右边的东西作为一个函数的返回值。
答:既然可以将任何可以放在=右边的东西作为一个函数返回值,那我们就写两个函数验证一下看看。
#-*- coding: utf-8 -*-
#验证return返回的例子
def return_a_string():
return "a string can be returned\n"
def return_two_strings():
return "a string can be returned\n", "another string can be returned\n"
def return_a_num():
return 2
def return_operator(a,b):
return a + b
def return_true():
return True
a = return_a_string()
b,c = return_two_strings()
d = return_two_strings()
e = return_a_num()
f = return_operator(3,4)
g = return_true()
print a,b,c,d,e,f,g
从上面的结果中可以看到,string和数字和True都可以作为返回值返回。但是在写接收值时,用一个变量接收的时候,换行符被完整地保留下来了,但是用两个参数接收时空格就被显示打印出来了。后来又试了一下d[0],d[1]打印出来的效果是一样的。
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
加分习题2:这个脚本的结尾是一个迷题。我将一个函数的返回值用作了另外一个函数的参数。我将它们链接到了一起,就跟写数学等式一样。这样可能有些难读,不过运行一下你就知道结果了。接下来,你需要试试看能不能用正常的方法实现和这个表达式一样的功能。
答:题目中“正常的方法”我理解为是用中间变量替代长长的调用函数的过程,所以只要把what中调用函数部分设置变量就行了。
para1 = multiply(weight, divide(iq,2))
para2 = subtract(height, para1)
what2 = add(age, para2)
print what2
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
加分习题3:一旦你解决了这个迷题,试着修改一下函数里的某些部分,然后看会有什么样的结果。你可以有目的地修改它,让它输出另外一个值。
答:这个习题是想让我们更深地理解return的用法,也就是一个函数的输出可以作为另一个函数的输入。
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
加分习题4:最后,颠倒过来做一次。写一个简单的等式,使用一样的函数来计算它。
答:不多说,贴代码。
#-*- coding: utf-8 -*-
def cal_rect_area(length,width):
return length * width
a=cal_rect_area(3.2,6.7)
print "the area of the rectangle is %f" %a
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总结:这一部分习题主要是为了学习函数返回return,return可以为空,也可以有返回值。就像书中写的,只要是可以放在等式右边的东西都能作为return的返回值。在使用的时候,可以直接调用返回值。