# -*- coding: utf-8 -*-
#第21节课练习
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)
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"
###############################################################
上面是我练习的代码 对着写的
现在问题来了 书上让我用 别的办法写
what = add(age,subtract(height,multiply(weight,divide(iq,2))))
得到同样的效果。。
这行代码看的我尤其的晕
我大概理解的就是
给what 赋值 add函数
add函数调用 age变量,跟subtract函数
也就是 age+subtract
而subtract也调用了 height,multiply 也就是
height - multiply
而 multiply 也调用了 weight变量 跟 divide函数
那么 multiply = weight * divide
而 divide 调用了 变量 iq 跟 2
也就是 iq / 2
那可不可以写成
print_one = multiply(weight,divide(iq,2))
print_two = subtract(height,print_one)
print_three = add(age,print_two)
这样写是可以运行。。不过我想问下对不对。。
#第21节课练习
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)
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"
###############################################################
上面是我练习的代码 对着写的
现在问题来了 书上让我用 别的办法写
what = add(age,subtract(height,multiply(weight,divide(iq,2))))
得到同样的效果。。
这行代码看的我尤其的晕
我大概理解的就是
给what 赋值 add函数
add函数调用 age变量,跟subtract函数
也就是 age+subtract
而subtract也调用了 height,multiply 也就是
height - multiply
而 multiply 也调用了 weight变量 跟 divide函数
那么 multiply = weight * divide
而 divide 调用了 变量 iq 跟 2
也就是 iq / 2
那可不可以写成
print_one = multiply(weight,divide(iq,2))
print_two = subtract(height,print_one)
print_three = add(age,print_two)
这样写是可以运行。。不过我想问下对不对。。