加分习题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 ~”
备注:个人学习记录,经验技术尚不足够,错的地方请大神指点,谢谢!