在python中用百分数分配和修改变量。

问题描述 投票:0回答:1

我是python的新手。我对变量和赋值很熟悉,至少我认为我很熟悉。我目前卡在下面的问题上。我不能为我的生活弄清楚为什么它是不正确的任何洞察力将是惊人的。提前感谢大家和任何人。

# 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*=(10/100)

# add the rainfall variable to the reservoir_volume variable
rainfall += reservoir_volume

# increase reservoir_volume by 5% to account for storm water that flows
# into the reservoir in the days following the storm
reservoir_volume *= (5/100)

# decrease reservoir_volume by 5% to account for evaporation
reservoir_volume *= (5/100)

# 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)
python variables percentage
1个回答
0
投票

你缺少一些基本的数学知识。如果你想增加5%的东西,你需要使用

volume += volume * 0.05

你在做什么(volumn *= (5/100))实际上是将音量设置为当前值的5%。另外,递减时一定要用负号。

# 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 -= reservoir_volume * (10/100)

# add the rainfall variable to the reservoir_volume variable
rainfall += reservoir_volume

# increase reservoir_volume by 5% to account for storm water that flows
# into the reservoir in the days following the storm
reservoir_volume += reservoir_volume * (5/100)

# decrease reservoir_volume by 5% to account for evaporation
reservoir_volume -= reservoir_volume * (5/100)

# 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)
© www.soinside.com 2019 - 2024. All rights reserved.