【发布时间】:2014-01-08 16:49:49
【问题描述】:
我正在尝试自学 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
【问题讨论】:
-
balance2是否曾经小于零? -
当 backPay >= 0, backPay += 1
-
如果您要开始学习代码,您还需要找到调试的好方法。尝试在您认为可能存在错误的代码的不同部分添加打印语句。从那里检查预期输入是否与实际输出相似。
标签: python loops while-loop terminate