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)
breakaffects the closest enclosing loop (theforin this case) - you never escape thewhile. Change your logic.breakout of the innerforloop, but you never break out of the outerwhileloop. Change your condition fromwhile Truetowhile balance >= 0and it should at least run.