笨方法学习Python 习题21 函数可以返回东西 ---学习记录

加分习题2:
what = add(age,subtract(height,multiply(weight,divide(iq,2))))
print “That becomes:”,what,”Can you do it by hand?”

等效于下面脚本:
Parameter1 = divide(iq,2)
Parameter2 = multiply(weight,Parameter1)
Parameter3 = subtract(height,Parameter2)
Parameter4 = add(age,Parameter3)
print “That becomes:”,Parameter4,”Can you do it by hand?”

对编写脚本进行注释:

定义四个函数 以及返回值

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 “Divideing %d / %d” % (a,b)
return a / b

打印内容

print “Let’s do same math with just functions!”

第一次调用add函数

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)

PUZZLE:使人疑惑的

print “Here is puzzle.”

35+(74-(180*(50/2)))—–后面会用其他的写法来表达这个计算式

what = add(age,subtract(height,multiply(weight,divide(iq,2))))

Parameter1 = divide(iq,2)
Parameter2 = multiply(weight,Parameter1)
Parameter3 = subtract(height,Parameter2)
Parameter4 = add(age,Parameter3)
print “That becomes:”,Parameter4,”Can you do it by hand?”

加分习题1
def Happy(a,b):
print”IF %r + %r ,we will Happyforever! ” % (a,b)
return a + b
def Forever(a,b):
print “IF %r + %r,we will Happyforever too!” % (b,a)
return b + a
def Disappearance_happiness(a,b):
print “If %r + %r ,we will lose all!” % (a,b)
return a - b

打印内容

print “Let’s Play a game!”

第一次调用函数

one = Happy(1,2)

第二次调用函数

two = Disappearance_happiness(3,2)
print “I wanted to let it be 2 equal you ,but I can’t write it ,sorry ~”

备注:个人学习记录,经验技术尚不足够,错的地方请大神指点,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值