我有一個問題,我現在開始學習 Python,并且正在網站上的測驗中撰寫代碼來學習 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-=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)
這是我的作業。我真的不知道錯誤在哪里
我用十進制寫錯了百分比嗎?我試過5和0.05都不起作用
是另一個嗎?不可能!
uj5u.com熱心網友回復:
為了減少rainfall10%,你寫了:
rainfall-=0.1
...改為嘗試:
#1
rainfall -= 0.1 * rainfall
或者,#2:
rainfall *= (1 - 0.1)
對以下幾行的類似更改也將有所幫助:
# 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
...即:
reservoir_volume = 0.05 * reservoir_volume
reservoir_volume -= 0.05 * reservoir volume
... 要么:
reservoir_volume *= (1 0.05)
reservoir_volume *= (1 - 0.05)
uj5u.com熱心網友回復:
另一種更簡單的方法是:
reservoir_volume *= 0.9
其他的將是:
reservoir_volume *= 1.05
reservoir_volume *= 0.95
主要的收獲是 = 和 -= 所做的是添加或減去一個平面值,而不是百分比。
uj5u.com熱心網友回復:
終于有答案了,非常感謝大家。這么簡單,容易迷路。。
@constantstranger 的回答很好地解釋了為什么與@A_Programmer 的回答相比,盡管在實踐中是不必要的
因為我添加了固定數字而不是百分比,所以我需要將變數更改為百分比。
100% 是 1。如果我想添加 10%,我需要添加 110%,即 1.1 減去 5% 將是 95%,所以 0.95,當然是乘以它,而不是添加它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/435879.html
上一篇:給定雙精度陣列,如何將最接近的整數列印為零?[復制]
下一篇:大整數的求和函式
