找出银行中的一笔钱通过年存款超过一定数量所需的年数

Find number of years it takes for an amount of money in a bank to pass a certain number with yearly deposit

First of January each year, Keven puts an amount of 10000 in a bank with an interest rate of 1.8 %. The first deposit was 01.01.2020. Make a Python program that calculates the number of years it takes for the amount of money to pass a certain number K. The input should be yearly deposit, interest rate, and K.The output should number of years.

我尝试使用 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)

如果在计算利息之前添加存款(如您的代码所示):

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