gpt4 book ai didi

python - 艰难地学习 Python 练习 21 额外学分

转载 作者:太空宇宙 更新时间:2023-11-04 09:06:51 25 4
gpt4 key购买 nike

我没有按照练习 21 中的以下部分进行操作,我猜是因为我的数学很弱。

# A puzzle for the extra credit, type it in anyway.

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

Description:

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.

我的问题是正常公式是什么,你是怎么算出来的?

最佳答案

你的代码行:

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

转换为:

age + (height - (weight * (iq / 2)))

由于操作顺序,可以简化为:

age + height - weight * iq / 2

或英文:

Age plus Height subtract Weight times half of IQ

我的方法是将语句扩展一点,以便于阅读:

第 1 步:

add(
age, subtract(
height, multiply(
weight, divide(
iq, 2
)
)
)
)

然后从最内层的语句开始翻译每个语句:

第 2 步:

add(
age, subtract(
height, multiply(
weight, (iq / 2)
)
)
)

第 3 步:

add(
age, subtract(
height, (weight * (iq / 2))
)
)

第 4 步:

add(
age, (height - (weight * (iq / 2)))
)

第 5 步:

age + (height - (weight * (iq / 2)))

编辑:

您需要基本了解:

multiply(x, y) is equivalent to x * y
add(x, y) is equivalent to x + y
subtract(x, y) is equivalent to x - y
divide(x, y) is equivalent to x / y

那么你还需要明白这些是可以组合的:

multiply(x, add(y, z)) is equivalent to multiply(x, (y + z)), and  x * (y + z)

我将 (y + z) 放在方括号中,以表明它应该首先计算,因为在嵌入式函数中总是首先计算内部值。

关于python - 艰难地学习 Python 练习 21 额外学分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19762491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com