Home > Software design >  Learning Python, and having a Math/Code problem
Learning Python, and having a Math/Code problem

Time:02-28

I have a problem, I am starting to learn Python right now and am writing code on a quiz in a website to learn python.

It is just math by know, and i am already in problems. sitting for half an hour or so on that.. or more..

# 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-=0.1
# 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 =0.05
# decrease reservoir_volume by 5% to account for evaporation
reservoir_volume-=0.05
# 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)

This is my job. And I really dont know where the mistake is

did I write the percentage in decimal wrong? I tried 5 aswell as 0.05 both dont work

is it another one? cant be!

CodePudding user response:

To reduce rainfall by 10%, you have written:

rainfall-=0.1

... instead try either:

#1

rainfall -= 0.1 * rainfall

or, #2:

rainfall *= (1 - 0.1)

Similar changes to the following lines will help as well:

# increase reservoir_volume by 5% to account for stormwater that flows
# into the reservoir in the days following the storm
reservoir_volume =0.05
# decrease reservoir_volume by 5% to account for evaporation
reservoir_volume-=0.05

... namely:

reservoir_volume  = 0.05 * reservoir_volume
reservoir_volume -= 0.05 * reservoir volume

... or:

reservoir_volume *= (1   0.05)
reservoir_volume *= (1 - 0.05)

CodePudding user response:

Another, simpler, way to do it would be:

reservoir_volume *= 0.9

The other ones would be:

reservoir_volume *= 1.05
reservoir_volume *= 0.95

The main takeaway from this is that what = and -= do is add or subtract a flat value, not a percentage.

CodePudding user response:

Finally have the answer thank you all very much. So simple, got lost easily..

Answer by @constantstranger was nice in explaining why though unnesecary in practice compared to the answer by @A_Programmer

since I added flat numbers instead of percentages I needed to change the variableinto a percentage.

100% is 1. If I want to add 10% I will need to ad 110% which is 1.1 Subtract 5% would be 95% so 0.95 and of course Multiply it, not add it.

  • Related