代码如下:把第3行的加号换成了减号

 1 def add(a, b):
 2     print "ADDING %d + %d" % (a, b)
 3     return a - b
 4 def substract(a, b):
 5     print "SUBTRACTING %d - %d" % (a, b)
 6     return a - b
 7 def multiply(a, b):
 8     print "MULTIPLYING %d * %d" % (a, b)
 9     return a * b
10 def divide(a, b):
11     print "DIVIDING %d / %d" % (a, b)
12     return a / b
13 
14 age = add(30, 5)
15 height = substract(78, 4)
16 weight = multiply(90, 2)
17 iq = divide(100, 2)
18 
19 print "Age: %d, Height: %d, Weight: %d, IQ:%d" % (age, height, weight, iq)
20 
21 what = add(age, substract(height, multiply(weight, divide(iq, 2))))
22 print "That becomes:", what, "Can you do it by hand?"

运行结果可见,return的值和定义变量里print那一行的内容无关

这里写图片描述


总觉得冗余的内容太多反而不利于理解,以下简化代码:

 1 def add(a, b):
 2     return a - b
 3 def substract(a, b):
 4     return a - b
 5 def multiply(a, b):
 6     return a * b
 7 def divide(a, b):
 8     return a / b
 9 
10 age = add(30, 5)
11 height = substract(78, 4)
12 weight = multiply(90, 2)
13 iq = divide(100, 2)
14 
15 print "Age: %d, Height: %d, Weight: %d, IQ:%d" % (age, height, weight, iq)
16 
17 what = add(age, substract(height, multiply(weight, divide(iq, 2))))
18 print "That becomes:", what, "Can you do it by hand?"

输出结果: 
这里写图片描述

posted on 2017-10-10 22:44  讲道理好嘛  阅读(184)  评论(0编辑  收藏  举报