每年 1 月 1 日,Keven 將 10000 美元存入銀行,利率為 1.8%。第一次存款是 01.01.2020。撰寫一個 Python 程式,計算金額通過特定數字 K 所需的年數。輸入應為年存款、利率和 K。輸出應為年數。
我嘗試了 while 回圈,但得到了無限回圈。
P=1.8 # Interest rate
D=10000 # Yearly deposit
K=250000
n=0 # n:number of years
A=D # A:amount of money in the bank
while A<=K:
n=n 1
D= A
A=D*(1 1.8/100) # Exponential growth
print(n)
uj5u.com熱心網友回復:
如果在計算利息之前添加存款(如您的代碼所示):
while A <= K:
n =1
A = (A D) * (1 P/100)
如果在計算利息后添加存款:
while A <= K:
n =1
A = A * (1 P/100) D
兩種變體都有效(它們對給定資料給出相同的答案 20,但對另一個輸入不同)
你的錯誤在這里
D= A
# but yearly deposit is constant
A=D*(1 1.8/100) # Exponential growth
#we should update current amount A with interest
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/323487.html
上一篇:用python生成馬爾可夫數列
