固定付款返回为0。为什么?要做什么更正?

2024-06-16 12:40:49 发布

您现在位置:Python中文网/ 问答频道 /正文

Now write a program that calculates the minimum fixed monthly payment needed in order pay off a credit card balance within 12 months. By a fixed monthly payment, we mean a single number which does not change each month, but instead is a constant amount that will be paid each month.

In this problem, we will not be dealing with a minimum monthly payment rate.

The following variables contain values as described below:

  • balance - the outstanding balance on the credit card
  • annualInterestRate - annual interest rate as a decimal

The program should print out one line: the lowest monthly payment that will pay off all debt in under 1 year, for example:

Lowest Payment: 180 

Assume that the interest is compounded monthly according to the balance at the end of the month (after the payment for that month is made). The monthly payment must be a multiple of $10 and is the same for all months. Notice that it is possible for the balance to become negative using this payment scheme, which is okay. A summary of the required math is found below:

  • Monthly interest rate = (Annual interest rate) / 12.0
  • Monthly unpaid balance = (Previous balance) - (Minimum fixed monthly payment)
  • Updated balance each month = (Monthly unpaid balance) + (Monthly interest rate x Monthly unpaid balance)

这是我的密码。我不知道我错在哪里:

balance = float(raw_input('enter the outsanding balance on your card'))
annualInterestRate  = float(raw_input('enter the anual interest rate as a decimal'))
month = 0
checkBalance = balance
monthlyFixedPayment = 0
while checkBalance <= 0:
    checkBalance = balance
    monthlyFixedPayment += 10
    while month <= 11:
        monthlyInterestRate = annualInterestRate/12.0
        monthlyUnpaidBalance = checkBalance - monthlyFixedPayment
        checkBalance = monthlyUnpaidBalance + (monthlyInterestRate * monthlyUnpaidBalance)
print('lowest payment:' + str(monthlyFixedPayment))

Tags: theforthatrateispaymentcardfixed
3条回答

我想这就是你要找的程序:

balance = 500
annualInterestRate = .5
checkBalance = balance
monthlyFixedPayment = 10
count = 0
while checkBalance > 0:
    month = 0
    while month <= 11 and checkBalance > 0:
        count+=1
        monthlyInterestRate = annualInterestRate/12.0
        monthlyUnpaidBalance = checkBalance - monthlyFixedPayment
        checkBalance = monthlyUnpaidBalance - (monthlyInterestRate * monthlyUnpaidBalance)
        print "\t"+str(checkBalance)
        month+=1
    print checkBalance

print "lowest amount: "
print count*monthlyFixedPayment+checkBalance

我留下了print语句,这样您就可以看到每次迭代中发生了什么。在

我在你的代码中发现了一些问题:

1)您正在执行一个monthlyFixedPayment += 10更改固定付款方式。你不应该根据你的问题定义改变固定付款。在

2)您在外部while循环的每个迭代中执行checkBalance = balance。这导致重置计算值。在

3)我引入了一个count变量来检查这些破坏发生了多少次,因为month在每次迭代中都会被重置。在

while checkBalance <= 0:到{}

另外,您需要在while month <= 11:循环中增加month。在

你的做法很困难;有一个固定付款的分析解决方案:

from math import ceil

def find_fixed_monthly_payment(balance, months, yearly_rate):
    i = 1. + yearly_rate / 12.
    im = i ** months
    return balance * (im * (1. - i)) / (i * (1. - im))

def find_final_balance(balance, months, yearly_rate, fixed_payment):
    i = 1. + yearly_rate / 12.
    for _ in range(months):
        # make payment
        balance -= fixed_payment
        # add interest
        balance *= i
    return balance

def get_float(prompt):
    while True:
        try:
            return float(raw_input(prompt))
        except ValueError:
            # input could not be cast to float; try again
            pass

def main():
    balance     = get_float("Please enter starting balance: ")
    annual_rate = get_float("Annual interest rate (in percent): ") / 100.

    fixed_payment = find_fixed_monthly_payment(balance, 12, annual_rate)

    # round up to the nearest $10
    fixed_payment = ceil(fixed_payment / 10.) * 10.

    # double-check value of fixed_payment:
    assert find_final_balance(balance, 12, annual_rate, fixed_payment      ) <= 0., "fixed_payment is too low!"
    assert find_final_balance(balance, 12, annual_rate, fixed_payment - 10.)  > 0., "fixed_payment is too high!"

    print("Lowest payment: ${:0.2f}".format(fixed_payment))

if __name__ == "__main__":
    main()

相关问题 更多 >