学习Python困难法练习21求助
大家好,我花了一些时间在这个问题上,但我觉得真的很难,搞不清楚。书上是这么说的:
在脚本的最后有一个谜题。 我把一个函数的返回值拿来当作另一个函数的参数。 我这样做是连着进行的, 就像在用这些函数创建一个公式。 看起来很奇怪,但如果你运行这个脚本,你就能看到结果。 你需要做的是试着找出一个正常的公式, 来重现这一系列的操作。
谢谢。
#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
在编程中,有时候我们会遇到一些问题,特别是在使用某些工具或库的时候。这些问题可能会让我们感到困惑,尤其是当我们刚开始学习编程的时候。比如,有人可能会在使用某个特定的功能时,发现它并没有按照预期工作。这时候,我们就需要去查找解决方案,看看有没有其他人遇到过类似的问题,并找到他们的解决办法。
在这个过程中,提问和回答是非常重要的。提问可以帮助我们更好地理解问题,而回答则能帮助其他人解决相似的困扰。通过这样的互动,大家都能学到更多的知识,提升自己的编程能力。
总之,编程的学习过程就是一个不断探索和解决问题的过程。遇到困难时,不要气馁,积极寻求帮助,最终你会找到解决方案的。
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)
1
“正常”的公式应该是:
what = (15+5) + (18-3) - (100*3)*(100/20)/2
1
你只需要一步一步地把这个函数里的内容(那些没有打印的语句)替换到最后的函数调用里。我来给你示范一下:
add(age, subtract(height, multiply(weight, devide(iq, 2))))
就变成了:
age + subtract(height, multiply(weight, devide(iq, 2)))