学习python艰苦的方式练习21帮助

时间:2011-03-08 20:47:26

标签: python

嘿伙计们,我花了一些时间在这一个,但我发现它真的很难,不能弄明白。这本书的内容如下:

  

在剧本的最后是一个谜题。   我正在拿一个的返回值   函数,并将其用作参数   另一个功能。我正在这样做   链条让我有点创造一个   公式使用函数。它看起来   真的很奇怪,但如果你跑了   脚本你可以看到结果。什么   你应该做的是试着找出答案   正常的公式,将重建   同样的一系列操作。

感谢。

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

3 个答案:

答案 0 :(得分:1)

您只需要通过将函数体(非打印语句)替换为最终函数调用来逐步执行此操作。我会开始你的:

添加(年龄,减去(高度,乘(重量,偏差(iq,2))))

变为:

age + subtract(height, multiply(weight, devide(iq, 2)))

答案 1 :(得分:1)

“正常”公式应为:

what = (15+5) + (18-3) - (100*3)*(100/20)/2

答案 2 :(得分:0)

above question you can only call the function. There is no arithmetic can't be performed at time of final function call. Because at final call if you try to add some arithmetic sign there will be show error like:

"**TypeError: unsupported operand type(s) for +: 'int' and 'tuple'"is question.**

There is shown little form of this question**

from sys import argv
#script=argv def add(a,b):
    print "Addition %d+%d" %(a,b)
    return a+b a=add(34,43) print "add=a %d" %(a) def sub(a,b):
    print "Addition %d-%d" %(a,b)
    return a-b b=sub(234,123) print "sub=a %d" %(b)

d=12 print "New experiment" c=add(a,sub(b,add(a,add(a,b)))) print "Final C=%d" %(c)