Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何添加计数器来跟踪一个while循环中的月份和年份?_Python_Python 3.x_Python 3.6 - Fatal编程技术网

Python 如何添加计数器来跟踪一个while循环中的月份和年份?

Python 如何添加计数器来跟踪一个while循环中的月份和年份?,python,python-3.x,python-3.6,Python,Python 3.x,Python 3.6,因此,我试图创建一个程序,我已经完成了大部分程序,但我在计数器方面遇到了一些问题。 -我需要添加一个数月和数年的计数器来跟踪成为百万富翁需要多长时间。 -我的月数计数器是正确的,但我很难算出年数计数器 以下是我目前的代码: balance = float(input("Enter initial amount: ")) monthlyContribution = float(input("Enter monthly contribution: ")) interestRate = float(i

因此,我试图创建一个程序,我已经完成了大部分程序,但我在计数器方面遇到了一些问题。 -我需要添加一个数月和数年的计数器来跟踪成为百万富翁需要多长时间。 -我的月数计数器是正确的,但我很难算出年数计数器

以下是我目前的代码:

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}')
余额=浮动(输入(“输入初始金额:”)
monthlyContribution=浮动(输入(“输入每月供款:”))
利率=浮动(输入(“输入年利率:”)
月份=0
年份=0
余额<1000000时:
月=月+1
年份=年份+1
利息=利率/100
余额=余额+月供款+(余额+月供款)*利息/12
打印(f'当前余额:${Balance:,.2f}',(f'在{month}个月之后'),(f'或{year}年')
打印(f'祝贺您,您将在{month}个月内成为百万富翁:${balance:,.2f}')

经过讨论,这里是最终结果:

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}')
余额=浮动(输入(“输入初始金额:”)
monthlyContribution=浮动(输入(“输入每月供款:”))
利率=浮动(输入(“输入年利率:”)
月份=0
利息=利率/100
余额<1000000时:
月=月+1
余额+=月分摊+(余额+月分摊)*利息/12
如果不是第%12个月:
年=月//12
rem=月份%12
在{month}或{year}年后打印(f'当前余额:${Balance:,.2f}+
f'和{rem}个月')
年=月//12
rem=月份%12
打印(f'\n预测,{month}个月后您将成为百万富翁'+
f'或{year}年和{rem}月'+
f'\n当前余额:${Balance:,.2f}')

@vash\u踩踏事件的答案有效。如果你想要一个完整的年数,你也可以在月数是12的倍数时增加年数

if month >= 12 and month % 12 == 0:
    year += 1

您的代码每年都在递增。这怎么可能是正确的?我是一个初学者编码器,所以我想学习。。。我需要另一个循环吗?你需要在头脑中尝试并执行代码。如果你看看你做了什么,你同时增加了月份和年份。所以你每走一步就搬了13个月。这似乎合理吗?这段代码告诉我,我真的必须继续做这个百万富翁的事情。我们还可以使用模数和地板除法,我在我的报告中补充说,我最喜欢这种方式,而不是用年作为小数。