艰苦学习Python练习21额外学分

艰苦学习Python练习21额外学分,python,Python,我想是因为我数学不好,所以我没有听懂练习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?" 说明: add( age,

我想是因为我数学不好,所以我没有听懂练习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?"
说明:

add(
    age, subtract(
        height, multiply(
            weight, divide(
                iq, 2
            )
        )
    )
)
add(
    age, subtract(
        height, multiply(
            weight, (iq / 2)
        )
    )
)
add(
    age, subtract(
        height, (weight * (iq / 2))
    )
)
add(
    age, (height - (weight * (iq / 2)))
)
age + (height - (weight * (iq / 2)))
剧本的结尾是一个谜。我取的是 一个函数,并将其用作另一个函数的参数。我是 在一个链中这样做,这样我就可以用 功能。这看起来很奇怪,但如果你运行脚本,你可以 看看结果。你应该做的是试着找出正常情况 重新创建同一组操作的公式

我的问题是什么是标准公式,您是如何计算出来的?

您的代码行:

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
            )
        )
    )
)
add(
    age, subtract(
        height, multiply(
            weight, (iq / 2)
        )
    )
)
add(
    age, subtract(
        height, (weight * (iq / 2))
    )
)
add(
    age, (height - (weight * (iq / 2)))
)
age + (height - (weight * (iq / 2)))
然后从最里面的语句开始翻译每条语句:

第二步:

add(
    age, subtract(
        height, multiply(
            weight, divide(
                iq, 2
            )
        )
    )
)
add(
    age, subtract(
        height, multiply(
            weight, (iq / 2)
        )
    )
)
add(
    age, subtract(
        height, (weight * (iq / 2))
    )
)
add(
    age, (height - (weight * (iq / 2)))
)
age + (height - (weight * (iq / 2)))
第三步:

add(
    age, subtract(
        height, multiply(
            weight, divide(
                iq, 2
            )
        )
    )
)
add(
    age, subtract(
        height, multiply(
            weight, (iq / 2)
        )
    )
)
add(
    age, subtract(
        height, (weight * (iq / 2))
    )
)
add(
    age, (height - (weight * (iq / 2)))
)
age + (height - (weight * (iq / 2)))
第4步:

add(
    age, subtract(
        height, multiply(
            weight, divide(
                iq, 2
            )
        )
    )
)
add(
    age, subtract(
        height, multiply(
            weight, (iq / 2)
        )
    )
)
add(
    age, subtract(
        height, (weight * (iq / 2))
    )
)
add(
    age, (height - (weight * (iq / 2)))
)
age + (height - (weight * (iq / 2)))
第五步:

add(
    age, subtract(
        height, multiply(
            weight, divide(
                iq, 2
            )
        )
    )
)
add(
    age, subtract(
        height, multiply(
            weight, (iq / 2)
        )
    )
)
add(
    age, subtract(
        height, (weight * (iq / 2))
    )
)
add(
    age, (height - (weight * (iq / 2)))
)
age + (height - (weight * (iq / 2)))
编辑:

add(
    age, subtract(
        height, multiply(
            weight, divide(
                iq, 2
            )
        )
    )
)
add(
    age, subtract(
        height, multiply(
            weight, (iq / 2)
        )
    )
)
add(
    age, subtract(
        height, (weight * (iq / 2))
    )
)
add(
    age, (height - (weight * (iq / 2)))
)
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)
周围,表示应该首先计算它,因为在嵌入函数中总是首先计算内部值。

您的代码行:

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
            )
        )
    )
)
add(
    age, subtract(
        height, multiply(
            weight, (iq / 2)
        )
    )
)
add(
    age, subtract(
        height, (weight * (iq / 2))
    )
)
add(
    age, (height - (weight * (iq / 2)))
)
age + (height - (weight * (iq / 2)))
然后从最里面的语句开始翻译每条语句:

第二步:

add(
    age, subtract(
        height, multiply(
            weight, divide(
                iq, 2
            )
        )
    )
)
add(
    age, subtract(
        height, multiply(
            weight, (iq / 2)
        )
    )
)
add(
    age, subtract(
        height, (weight * (iq / 2))
    )
)
add(
    age, (height - (weight * (iq / 2)))
)
age + (height - (weight * (iq / 2)))
第三步:

add(
    age, subtract(
        height, multiply(
            weight, divide(
                iq, 2
            )
        )
    )
)
add(
    age, subtract(
        height, multiply(
            weight, (iq / 2)
        )
    )
)
add(
    age, subtract(
        height, (weight * (iq / 2))
    )
)
add(
    age, (height - (weight * (iq / 2)))
)
age + (height - (weight * (iq / 2)))
第4步:

add(
    age, subtract(
        height, multiply(
            weight, divide(
                iq, 2
            )
        )
    )
)
add(
    age, subtract(
        height, multiply(
            weight, (iq / 2)
        )
    )
)
add(
    age, subtract(
        height, (weight * (iq / 2))
    )
)
add(
    age, (height - (weight * (iq / 2)))
)
age + (height - (weight * (iq / 2)))
第五步:

add(
    age, subtract(
        height, multiply(
            weight, divide(
                iq, 2
            )
        )
    )
)
add(
    age, subtract(
        height, multiply(
            weight, (iq / 2)
        )
    )
)
add(
    age, subtract(
        height, (weight * (iq / 2))
    )
)
add(
    age, (height - (weight * (iq / 2)))
)
age + (height - (weight * (iq / 2)))
编辑:

add(
    age, subtract(
        height, multiply(
            weight, divide(
                iq, 2
            )
        )
    )
)
add(
    age, subtract(
        height, multiply(
            weight, (iq / 2)
        )
    )
)
add(
    age, subtract(
        height, (weight * (iq / 2))
    )
)
add(
    age, (height - (weight * (iq / 2)))
)
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)
周围,以表明应该首先计算它,因为在嵌入函数中总是首先计算内部值。

正常公式是: 年龄+(身高-(体重*(iq/2)))

至于原因,请从代码开始:

add(age, subtract(height, multiply(weight, divide(iq, 2))))
此代码将首先执行
divide(iq,2)
,给我们(iq/2)。为了可视化,我将用其“正常”结果替换函数:

有了这个值,
就可以计算出乘以(重量,(iq/2))
。因此,
weight
iq/2
相乘--
weight*(iq/2)
。同样,将函数替换为“正常”结果:

现在计算'subtract(身高,(体重*(iq/2)),从第一个参数中减去第二个参数:

add(age, (height - (weight * (iq/2))))
最后,计算
add()
,并将
age
添加到等式的其余部分,因此您的最终“正常”结果是:

age + height - (weight * iq/2)
“正常公式”是: 年龄+(身高-(体重*(iq/2)))

至于原因,请从代码开始:

add(age, subtract(height, multiply(weight, divide(iq, 2))))
此代码将首先执行
divide(iq,2)
,给我们(iq/2)。为了可视化,我将用其“正常”结果替换函数:

有了这个值,
就可以计算出乘以(重量,(iq/2))
。因此,
weight
iq/2
相乘--
weight*(iq/2)
。同样,将函数替换为“正常”结果:

现在计算'subtract(身高,(体重*(iq/2)),从第一个参数中减去第二个参数:

add(age, (height - (weight * (iq/2))))
最后,计算
add()
,并将
age
添加到等式的其余部分,因此您的最终“正常”结果是:

age + height - (weight * iq/2)

它从哪里开始?换句话说,运算顺序是什么?从左到右计算乘法和除法,然后从左到右执行加法/减法,但在原始语句中,它将首先执行最内部的方法。在这种情况下,
div(iq,2)
iq/2
您是否理解
乘法(x,y)
x*y
相同?作为扩展,
multiply(x,add(y,z))
x*(y+z)
是一样的,我不明白。我在学校代数不好,从哪里开始?换句话说,运算顺序是什么?从左到右计算乘法和除法,然后从左到右执行加法/减法,但在原始语句中,它将首先执行最内部的方法。在这种情况下,
div(iq,2)
iq/2
您是否理解
乘法(x,y)
x*y
相同?作为扩展,
multiply(x,add(y,z))
x*(y+z)
是一样的,我不明白。我在学校代数不好。