【问题标题】:Write a program that calculates the minimum fixed monthly payment needed in order pay off a credit card balance within 12 months编写一个程序,计算在 12 个月内还清信用卡余额所需的最低每月固定还款额
【发布时间】:2012-10-06 02:25:12
【问题描述】:

这个问题是针对 python 2.7 的。问题要求编写一个程序,计算在 12 个月内还清信用卡余额所需的最低每月固定还款额。固定每月还款额是指一个每月不变的数字,而是一个常数每月支付的金额。

在这个问题中,我们不会处理最低月付款率。

以下变量包含如下所述的值: balance - 信用卡上的未结余额 yearInterestRate - 以小数表示的年利率

该程序应打印出一行:将在 1 年内偿还所有债务的最低每月付款。

假设利息按月末余额按月复利(当月还款后)。每月付款必须是 10 美元的倍数,并且所有月份都相同。请注意,使用此付款方案,余额可能会变为负数,这没关系。所需数学的摘要如下:

月利率 = (年利率) / 12 每月更新余额 =(上一次余额 - 每月最低还款额)x(1 + 月利率)

我想出了问题的代码;但是,我反复遇到无限循环。

    b = balance = 3329
    air = annualInterestRate = 0.2
    monthlyInterestRate = (air/12)
    mmp = minimumMonthlyPayment = (balance * monthlyInterestRate)
    month = 0
    while month <= 12:
        b = ((b - mmp) * (1 + (air/12)))
        month = 1 + month
        if b <= 0 and month == 12:
           break
        elif b > 0 and month == 12:
           b = balance
           month = 0
           mmp = minimumMonthlyPayment + 10.00
    print str('Lowest Payment: ' + str(round(mmp, 2)))

有人可以帮我修复此代码吗?对于给定的余额,最低付款是 310...我不知道如何获得这个...

【问题讨论】:

    标签: python python-2.7


    【解决方案1】:
    monthlyInterestRate = annualInterestRate/12
    monthlyPayment = 0
    newbalance = balance
    while newbalance > 0:
        monthlyPayment += 10
        newbalance = balance
        month = 1
    
        while month <= 12 and newbalance > 0:
            newbalance -= monthlyPayment
            interest = monthlyInterestRate * newbalance
            newbalance += interest
            month += 1
        newbalance = round(newbalance,2)
    

    【讨论】:

      【解决方案2】:
      monthlyPayment = 0
      monthlyInterestRate = annualInterestRate /12
      newbalance = balance
      month = 0
      
      while newbalance > 0:
          monthlyPayment += 10
          newbalance = balance
      
          for month in range(1,13):
              newbalance -= monthlyPayment
              newbalance += monthlyInterestRate * newbalance
              month += 1
      print " Lowest Payment:", monthlyPayment
      

      【讨论】:

        【解决方案3】:

        这段代码有点奇怪,就是这样的行:

           mmp = minimumMonthlyPayment = (balance * monthlyInterestRate)
        

        带有双等号。

        这段代码不会卡住:

        balance = 5000
        annualInterestRate = 0.2
        monthlyInterestRate = (annualInterestRate/12)
        minimumMonthlyPayment = (balance * monthlyInterestRate)
        month = 0
        while month <= 12:
            balance = ((balance - minimumMonthlyPayment) * (1 + monthlyInterestRate))
             month = 1 + month
            if balance <= 0 and month == 12:
                break
            elif balance > 0 and month == 12:
                month = 0
                minimumMonthlyPayment + 1.00
        
        print str('Lowest Payment: ' + str(round(minimumMonthlyPayment, 2)))
        

        但没有返回您正在寻找的答案。

        【讨论】:

        • 好吧,老师放了很多东西来代替 balance 和 AnnualInterestRate,只要它检查正确我很好......但是猜测和检查程序(如你所见)不是写的真好……
        • 最后一个代码(来自 Dan Steingart)也不起作用,例如:balance = 4773,annualInterestRate = 0.2。结果给出的值是 450,而不是 440!!
        【解决方案4】:

        首先你必须修正月份

        试试这个:

            balance = 3329
            annualInterestRate = 0.2
        
            monthlyPayment = 10
            monthlyInterestRate = interestRate/12
            newbalance = balance - 10
        
        
            while newbalance > 0:
                monthlyPayment += 10
                newbalance = balance
                month = 0
        
                while month < 12 and newbalance > 0:
        
                    month += 1
                    interest = monthlyInterestRate * newbalance
                    newbalance -= monthlyPayment
                    newbalance += interest
        
            newbalance = round(newbalance,2)
        
        
            print " Lowest Payment:", monthlyPayment
        

        【讨论】:

          【解决方案5】:

          这应该可以在所有情况下为您提供正确的答案:

          monthlyPayment = 10
          monthlyInterestRate = annualInterestRate /12
          newbalance = balance - 10
          
          while newbalance > 0:
              monthlyPayment += 10
              newbalance = balance
              month = 0
          
              while month < 12 and newbalance > 0:
                  newbalance -= monthlyPayment
                  interest = monthlyInterestRate * newbalance
                  newbalance += interest
                  month += 1
              newbalance = round(newbalance,2)
          print " Lowest Payment:", monthlyPayment
          

          【讨论】:

            【解决方案6】:
            balance = 3329
            annualInterestRate = 0.2
            monthlyInterestRate = annualInterestRate/12
            monthlyPayment = 0
            newbalance = balance
            while newbalance > 0:
            monthlyPayment += 10
            newbalance = balance
            month = 1
            while month <= 12 and newbalance > 0:
                newbalance -= monthlyPayment
                newbalance += (monthlyInterestRate * newbalance)
                month += 1    
            print "Lowest Payment:",monthlyPayment
            

            认为这将是您疑问的最佳解决方案..并满足答案

            【讨论】:

              【解决方案7】:

              我认为这是一种相当简单的方法:

              x = 50
              o = balance
              a = [1,2,3,4,5,6,7,8,9,10,11,12]
              monthly = annualInterestRate/12
              
              while balance>0:
                  for months in a:
                      u = balance - x
                      balance = u + (monthly * u)
                  if balance > 0:
                      x += 10
                  else:
                      break
              print x
              

              【讨论】:

                【解决方案8】:

                这基本上是 randomizertech 答案的略微改进版本,它允许用户输入不同的变量,而不是只为初始余额和年利率的固定值输入。 最后它会打印出更多对用户有用的变量。

                InitialBalance = float(raw_input("Enter your current balance: "))
                InterestRate = float(raw_input("Enter the yearly interest rate as a decimal: "))
                
                
                monthlyPayment = 0
                monthlyInterestRate = InterestRate/12
                balance = InitialBalance
                
                
                while balance > 0:
                    monthlyPayment += 10
                    balance = InitialBalance
                    numMonths = 0
                
                    while numMonths < 12 and balance > 0:
                
                        numMonths += 1
                
                        interest = monthlyInterestRate * balance
                
                        balance -= monthlyPayment
                
                        balance += interest
                
                    balance = round(balance,2)
                
                print "RESULT"
                print "Monthly payment to pay off debt in 1 year: " , monthlyPayment
                print "Number of months need to pay the debt: " , numMonths
                print "Balance after debt is paid: " , balance
                

                【讨论】:

                • 能否请您 edit 解释为什么这段代码回答了这个问题?纯代码答案是 discouraged,因为它们不教授解决方案。
                • 对不起,这里的菜鸟。我实际上是在寻找类似问题的解决方案,其中每月付款是通过二等分获得的,而不是增加 10。然后我遇到了这个问题,所以我只是粘贴了我所拥有的。刚刚意识到非常相似的代码也已经发布了。
                【解决方案9】:
                for i in range(12):
                     balance = balance - (balance * monthlyPaymentRate) + ((balance - (balance * monthlyPaymentRate)) * (annualInterestRate/12))
                
                print("Remaining balance:", round(balance, 2))
                

                正确 10/10

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2016-05-06
                  • 2017-12-06
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多