解决方案:赋值和修改变量
下面是上道练习的解决方案:
# The current volume of a water reservoir (in cubic metres)
reservoir_volume = 4.445e8
# The amount of rainfall from a storm (in cubic metres)
rainfall = 5e6
# decrease the rainfall variable by 10% to account for runoff
rainfall *= .9
# add the rainfall variable to the reservoir_volume variable
reservoir_volume += rainfall
# increase reservoir_volume by 5% to account for stormwater that flows
# into the reservoir in the days following the storm
reservoir_volume *= 1.05
# decrease reservoir_volume by 5% to account for evaporation
reservoir_volume *= 0.95
# subtract 2.5e5 cubic metres from reservoir_volume to account for water
# that's piped to arid regions.
reservoir_volume -= 2.5e5
# print the new value of the reservoir_volume variable
print(reservoir_volume)
解决方案:更改变量
对于第一个多选题练习,正确答案是 int(mv_density) 的值没有变化。它依然是 6229。
因为当变量被赋值时,赋给了右侧的表达式的值,而不是表达式本身。在下面的行中:
mv_density = mv_population/mv_area
Python 实际上计算了右侧表达式 mv_population/mv_area 的结果,然后将变量 mv_density 赋为该表达式的值。它立即忘记该公式,仅将结果保存到变量中。
考虑到 mv_population 的变化,为了更新 mv_density 的值。我们需要再次运行下面这行:
mv_density = mv_population/mv_area
print(int(mv_density))
6252
这是人们往返城市后,出现的新人口密度。所有变量都已更新为 mv_population 变化后的对应结果。
网友评论