Reputation: 1879
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)
Upvotes: 0
Views: 290
Reputation: 1879
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)
Upvotes: 1