2 回答

TA贡献1812条经验 获得超5个赞
经过这里的讨论是最终结果:
balance = float(input("Enter initial amount: "))
monthlyContribution = float(input("Enter monthly contribution: "))
interestRate = float(input("Enter annual interest rate: "))
month = 0
interest = interestRate/100
while balance < 1000000 :
month = month + 1
balance += monthlyContribution + (balance + monthlyContribution) * interest/12
if not month % 12:
year = month//12
rem = month % 12
print(f'Current Balance: ${balance:,.2f} after {month} or {year} years' +
f'and {rem} months')
year = month//12
rem = month % 12
print(f'\nCongratulations, you will be a millionaire in {month} months' +
f' or {year} years and {rem} months' +
f'\nCurrent Balance: ${balance:,.2f}')

TA贡献1813条经验 获得超2个赞
如果您想拥有整数年,您还可以在月份是 12 的倍数时增加年份的计数器。
if month >= 12 and month % 12 == 0:
year += 1
添加回答
举报