【发布时间】:2021-10-17 12:40:03
【问题描述】:
我有工作代码,但它每年都给我相同的Invest_amount(没有添加上一年的兴趣),所以我创建了一个Starting_balance 来尝试解决这个问题。
但是,现在它给了我这个错误信息:
TypeError: can only concatenate str (not "int") to str. But I put str in front of the value?
我的代码:
InvestAmount = int(input("Enter the intial investment amount: "))
Years = int(input("Enter the number of years to invest: "))
Rate = float(input("Enter the intrest rate (as %): "))
TotalInterestEarned = 0
for i in range(Years):
InterestEarned = round(InvestAmount*(Rate/100),2)
EndingBal = round(InvestAmount+InterestEarned , 2)
Starting_balance = (InvestAmount + InterestEarned)
print("Starting Balance: for year " + (i+1)+"$"+str(Starting_balance))
print("Ending balance: for year" +(i+1)+"$"+str(EndingBal))
print("Total interest earned: for year" +(i+1)+"$"+str(InterestEarned))
【问题讨论】:
-
很可能是在抱怨
(i + 1)。 -
(i+1)是一个int。就让它str(i+1)。它会将其转换为字符串。