Question

I am trying to teach myself python and have no experience write code. For my first attempt I am trying to write a program that applies the snowball principle to debt reduction, but also adds in an extra set amount each payment. I can get the first debt to clear(it goes to a negative but it exits the loop). My second step wont exit the loop and I have looked at topics that dealt with nested loops but they did not help. Could someone please show me where I went wrong?

#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
Was it helpful?

Solution

Take a look at this loop:

while backPay >= 0:
    balance2 -= standPay2
    backPay += 1

Here, backPay in increased in each iteration, so the condition backPay >= 0 will always be true.

Not sure what the code is intended to do, but probably you have to do backPay -= 1 instead. However, note that since the number of iterations of the loop is known beforehand, and you are just adding a fixed number in each iteration, you could just as well replace the loop with a simple multiplication.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top