Python 编写一个程序,计算在 12 个月内还清信用卡余额所需的最低每月固定付款额
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12907040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write a program that calculates the minimum fixed monthly payment needed in order pay off a credit card balance within 12 months
提问by
This question is for python 2.7. The question asks to 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.
这个问题是针对python 2.7的。该问题要求编写一个程序来计算在 12 个月内还清信用卡余额所需的最低固定月供。每月将支付的金额。
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
以下变量包含如下所述的值: balance - 信用卡上的未偿余额 yearInterestRate - 年利率为小数
The program should print out one line: the lowest monthly payment that will pay off all debt in under 1 year.
该程序应打印出一行:将在 1 年内还清所有债务的最低每月付款。
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:
假设利息按月末(当月还款后)的余额按月复利。每月付款必须是 10 美元的倍数,并且所有月份都相同。请注意,使用此付款方案,余额可能会变为负数,这是可以的。所需数学的摘要如下:
Monthly interest rate = (Annual interest rate) / 12 Updated balance each month = (Previous balance - Minimum monthly payment) x (1 + Monthly interest rate)
月利率 = (年利率) / 12 每月更新余额 = (以前余额 - 每月最低还款额) x (1 + 月利率)
I came up with a code for the question; however, I repeatedly got an infinite loop.
我想出了一个问题的代码;然而,我一再得到一个无限循环。
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)))
Can someone help me fix this code? For the given balance the lowest payment is 310...I'm not sure how to get this...
有人可以帮我修复此代码吗?对于给定的余额,最低付款是 310 ……我不知道如何得到这个……
回答by Dan Steingart
This code is a bit weird, namely lines like:
这段代码有点奇怪,即如下几行:
mmp = minimumMonthlyPayment = (balance * monthlyInterestRate)
with the double equal signs.
与双等号。
This code doesn't get stuck:
这段代码不会卡住:
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)))
But doesn't return the answer you're looking for.
但不会返回您正在寻找的答案。
回答by Hisham
First of all you have to fix month <= 12 make it month < 12 secondly you are using a form which is not needed here balance = ((balance - minimumMonthlyPayment) * (1 + monthlyInterestRate)) because simply you don't have to use the minimum payment here Your code is too bad
首先,您必须修复月 <= 12 使其成为月 < 12 其次,您使用的是此处不需要的表格 balance = ((balance - minimumMonthlyPayment) * (1 + MonthlyInterestRate)) 因为您不必在此处使用最低付款额您的代码太糟糕了
Try this:
尝试这个:
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
回答by Kamal
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)
回答by randomizertech
This should give you the right answer in all of the cases:
在所有情况下,这应该给你正确的答案:
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
回答by Bestin John
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
Think this will be the best solution to your doubt..and satisfies the answer
认为这将是您的疑问的最佳解决方案......并满足答案
回答by Bestin John
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
回答by Krzysztof Olszewski
I think this is a fairly simple way of doing it:
我认为这是一个相当简单的方法:
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
回答by ZeeCpt
This is basically a slightly improved version of randomizertech's answer, which allows user to input different variables instead of only doing it for fixed values of Initial balance and Annual interest rate. And in the end it prints some more variables that would be useful to a user.
这基本上是 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
回答by Py_Padawan
for i in range(12):
balance = balance - (balance * monthlyPaymentRate) + ((balance - (balance * monthlyPaymentRate)) * (annualInterestRate/12))
print("Remaining balance:", round(balance, 2))

