【笨方法学python】ex21 - 函数可以返回东西
代码如下:
点击查看代码
# 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) # age = 30 + 5 = 35
height = subtract(78, 4) # height = 78 - 4 = 74
weight = multiply(90, 2) # weight = 90 * 2 = 180
iq = divide(100, 2) # iq = 100 / 2 = 50
print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)
# 输出 age height weight iq
# A puzzle for the extra credit, type it in any way.
print "Here is a puzzle."
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
# what = 35 + (74 - (180 * (50 / 2) ) ) = -4391
print "That becomes: ", what, "Can you do it by hand?"
执行结果: