0

The code below doesn't work and I'm very frustrated. I know the correct output should be $310 but somehow my code isn't getting there. This is homework for an edex course intro to CS and python. I've tried to comment what I think the code is doing but clearly i'm not right.

Any help or hints would be very much appreciated.

balance = 3329
annualInterestRate = 0.2
monthInterest = annualInterestRate/12.0

# simple payment for the month
monthlyPaymentRate = 10

# while this is true, run the for loop from 1 - 12. This loop will break if the balance gets <     0, otherwise the 
# monthly payment rate adds by 10 each year. 
while True:
    for month in range(1, 13):
        balance = balance - monthlyPaymentRate
        interestBalance = balance + (monthInterest*balance) 
        balance = interestBalance
        if balance < 0:
            break
        else:
            monthlyPaymentRate += 10

print "balance = " + str(balance)
print "annualInterestRate = " + str(annualInterestRate)

print"Lowest payment: " + str(monthlyPaymentRate)
5
  • Give us some info about the actual problem. What is the problem you are trying to solve? Commented Sep 9, 2014 at 4:35
  • 3
    break affects the closest enclosing loop (the for in this case) - you never escape the while. Change your logic. Commented Sep 9, 2014 at 4:35
  • 1
    You break out of the inner for loop, but you never break out of the outer while loop. Change your condition from while True to while balance >= 0 and it should at least run. Commented Sep 9, 2014 at 4:36
  • Why do you even need a outer while loop ? Also, can you explain, what this code is expected to do. Commented Sep 9, 2014 at 6:23
  • Sorry, the code is trying to calculate the lowest possible amount that I can pay off the variable balance in 12 months with an annual interest rate of 20% or 0.2. I was trying to test the variable monthlyPaymentRate with an initial value of $10 then if there that didn't do it, increment up by $10 until the balance was paid off in 12 months. Commented Sep 9, 2014 at 15:40

1 Answer 1

1

Thank you very much for the comments, I was able to adjust my code to get the appropriate results, by making a function that meant I could test the results of a monthly payment rate over the course of a year, return a result and if that result wasn't what I wanted would rerun the code in the while loop. Tricky business this learning to code but quite fun.

Any thoughts on cleanliness or efficiency would be most welcome, pretty sure this isn't the most efficient step.

balance = 4400
annualInterestRate = 0.18
monthInterest = annualInterestRate/12.0

# simple payment for the month
monthlyPaymentRate = 0

# while the balance is greater than zero, run the for loop from 1 - 12. This loop will break if 
# the balance gets <=0, otherwise the monthly payment rate adds by 10 each year.
while balance > 0:
    monthlyPaymentRate += 10
    def testBalance(balance, monthlyPaymentRate):
        for month in range(1, 13):
            balance = balance - monthlyPaymentRate
            interestBalance = balance + (monthInterest*balance)
            balance = interestBalance
        return balance

    if testBalance(balance, monthlyPaymentRate) <= 0:
        break

print"Lowest Payment: " + str(monthlyPaymentRate)
Sign up to request clarification or add additional context in comments.

3 Comments

A more pythonic way for printing output and avoiding adding spaces inside of quotations would be: print "Lowest Payment:", monthlyPaymentRate or print "Lowest Payment: %s" % monthlyPaymentRate
Also define your function or method testBalance outside of the while loop, that wastes resources by redefining the same function over and over again inside a loop.
and btw this isn't a problem you need to solve programmatically. This is algebra (albeit advanced). See here for more info

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.