ex21 函数可以返回某些东西

ex21

在创建函数之后,通过 return 可以让函数返回一些值。

#-*- 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 "MULTIPLY %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 match 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)

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?"

#通过正常的方法实现了puzzle里边的功能
divd = divide(iq,2)

multip = multiply(weight,divd)

subsrt = subtract(height,multip)

ad = add(age,subsrt)
print "That becomes",what,"Can you do it by hand?"

 
原文地址:https://www.cnblogs.com/dingtou00/p/7708342.html