For循环只增加两个变量之一

时间:2017-08-25 14:55:35

标签: python python-3.x

我希望结果显示每年增加利息后的总金额,但它只会增加年份而不是金额。为什么呢?

while True:
    try:
        investment = float(input('How much to invest : '))
        interest = float(input('Interest rate : '))
        break
    except ValueError:
        "Please enter a valid number"

for year in range(10):
    money = investment + (investment * interest)
    print("Total money in year {} : {}".format((year+1), money))

2 个答案:

答案 0 :(得分:2)

听起来你需要产生兴趣:

for year in range(10):
    investment += (investment * interest)
    print("Total money in year {} : {}".format((year + 1), investment))

答案 1 :(得分:1)

逻辑错误。您的investment变量不会在循环中的每一轮分配。