hey guys, i spent some time on this one but i find it really hard and cant figure it out. heres what the book says:
At the end of the script is a puzzle.
I’m taking the return value of one
function, and using it as the argument
of another function. I’m doing this in
a chain so that I’m kind of creating a
formula using the functions. It looks
really weird, but if you run the
script you can see the results. What
you should do is try to figure out the
normal formula that would recreate
this same set of operations.
thanks.
#functions can return something too
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 devide(a, b):
print 'DEVIDING %d * %d' % (a, b)
return a / b
age = add(15, 5)
height = subtract(18, 3)
weight = multiply(100, 3)
iq = devide(100, 20)
print 'age: %d, height: %d, weight: %d, iq: %d' % (age, height, weight, iq)
#puzzle for extra credit
what = add(age, subtract(height, multiply(weight, devide(iq, 2))))
print "That becomes: ", what, "Can you do it by hand?"
You just need to step through this by substituting the body of the function (the non-print statements) into the final function call. I’ll start you out:
add(age, subtract(height, multiply(weight, devide(iq, 2))))
becomes: