因此,我试图创建一个程序,我已经完成了程序的大部分,但我有一些计数器的问题。 -我需要添加一个数月或数年的计数器,用来跟踪成为百万富翁需要多长时间。 -我的月数计数器是正确的,但我很难算出年数计数器。你知道吗
以下是我目前的代码:
balance = float(input("Enter initial amount: "))
monthlyContribution = float(input("Enter monthly contribution: "))
interestRate = float(input("Enter annual interest rate: "))
month = 0
year = 0
while balance < 1000000 :
month = month + 1
year = year + 1
interest = interestRate/100
balance = balance + monthlyContribution + (balance + monthlyContribution) * interest/12
print(f'Current Balance: ${balance:,.2f}', (f'after {month} months'), (f' or {year} years'))
print(f'Congratulations, you will be a millionaire in {month} months: ${balance:,.2f}')
@vashïu踩踏者的答案有效。如果你想有一个完整的年数,你也可以在月是12的倍数时增加年的计数器。你知道吗
经过讨论,这里是最终结果:
相关问题 更多 >
编程相关推荐