我正在尝试自学python并且没有编写代码的经验。对于我的第一次尝试,我正在尝试编写一个程序,将雪球原则应用于减债,但每次付款还会增加额外的设定金额。我可以得到第一笔债务清算(它会变成负数,但它会退出循环)。我的第二步不会退出循环,我已经看过处理嵌套循环的主题,但它们没有帮助。有人可以告诉我哪里出错了吗?
#Temp fixed number for testing use rawinput for actual program.
#name the debt
debt1 = "CC A"
#currnet balnace
balance1 = float(5000)
#APR
annualInterestRate1 = float(.1499)
#Currnet Monthly Payment
minMonthlyPayment1 = float(200)
# Exta Payment
boosterPayment = float(337)
print "The balance on ",debt1," is ",balance1
debt2 = "CC B"
balance2 = float(1000)
annualInterestRate2 = float(.1499)
minMonthlyPayment2 = float(200)
print "The balance on ",debt2," is ",balance2
debt3 = "ICCU"
balance3 = float(6000)
annualInterestRate3 = float(.0879)
minMonthlyPayment3 = float(130)
print "The balance on ",debt3," is ",balance3
debt4 = "Car"
balance4 = float(8000)
annualInterestRate4 = float(.0699)
minMonthlyPayment4 = float(200)
print "The balance on ",debt4," is ",balance4
debt5 = "Truck"
balance5 = float(15000)
annualInterestRate5 = float(.0439)
minMonthlyPayment5 = float(333)
#nubmer of payments made durning the debt reduction. Used as the index.
numPay = 0
save = 0
#For Debt1 with an APR greater then 0
intPayment1 = round(balance1*(annualInterestRate1/12),2)
while balance1 >= 0:
#payment with intrest
payment1 = minMonthlyPayment1 - intPayment1 + boosterPayment
#subtact payment from balance
balance1 -= payment1
#count Number of payments
numPay += 1
print numPay
print balance1
#For Debt2 with an APR greater then 0
#Figures monthly charge based on given APR
intPayment2 = round(balance2*(annualInterestRate2/12),2)
#Monthly payment minus intrest
standPay2 = minMonthlyPayment2 - intPayment2
while balance2 >= 0:
#payment while debt1 is being paid
#need a way to pay the payments while the other debt is being figured
backPay = numPay
while backPay >= 0:
balance2 -= standPay2
backPay += 1
#payment with intrest takes 100 away for savings
payment2 = minMonthlyPayment2 - intPayment2 + (boosterPayment-100)
#subtact payment from balance
balance2 -= payment2
#count Number of payments
numPay += 1
#keep track of how much is going to savings
save += 100
print numPay
print balance1
print save
答案 0 :(得分:1)
看看这个循环:
while backPay >= 0:
balance2 -= standPay2
backPay += 1
此处,backPay
在每次迭代中都会增加,因此条件backPay >= 0
将始终为真。
不确定代码的用途,但可能需要执行backPay -= 1
。但是请注意,由于循环的迭代次数是预先知道的,并且您只是在每次迭代中添加一个固定的数字,所以您也可以用简单的乘法替换循环。