I am trying to solve the second problem on the first problem set ("Paying Debt Off In a Year"). Apparently there's some error in my code. Can anyone please check the code and tell me the flaw? Thanks in advance.
balance = float(raw_input('Enter the outstanding balance on your credit card: ')) ann_rate = float (raw_input('Enter the annual credit card interest rate as a decimal: ')) month_rate = ann_rate/12 i = 0 t = balance while t > 0: t = balance i += 10 for j in range (1, 13): t = t * (1 + month_rate) - i print 'RESULT' print 'Monthly payment to pay off debt in 1 year: ', i print 'Number of months needed: ', j print 'Balance: ', t
I think I figured this on my own. The inner for loop didn't check for the value of t. So whenever the t became negative, it would continue counting until the end of the loop. I rewrote this using two nested while statements that check the value of t: balance = float(raw_input('Enter the outstanding balance on your credit card: ')) ann_rate = float (raw_input('Enter the annual credit card interest rate as a decimal: ')) month_rate = ann_rate/12 i = 0 t = balance while t > 0: j = 0 t = balance i += 10 while (j < 12) and (t > 0): t = t * (1 + month_rate) - i j += 1 ## print 'balance: ', t ## print 'minimum pay: ', i ## print '-----' print 'RESULT' print 'Monthly payment to pay off debt in 1 year: ', i print 'Number of months needed: ', j print 'Balance: ', round(t, 2)
please use a code pasting site and selct python syntax highlighting
code pasting sites 1. pastebin.com 2. dpaste.com
Thanks. So after the second one, I am working on the 3rd problem and I am badly stuck this time (same problem, but using bisection method): http://dpaste.com/1007290/ Can anyone please read the code and give me hints ? I wanna know what goes wrong here.
why are you using twelve times ans in all of your tests. A good way to start tackling a problem is to write down the steps you need to perform to solve the problem. 1 - initialize parameters 2 - establish an initial upper and lower bound 3 - calculate a guess that is half-way between the bounds 3 - run the guess through 12 payment cycles 4 - test the result 5 - if the guess was too big, make the upper bound equal to the guess and repeat at 3 6 - if the guess was too small, make the lower bound equal to the guess and repeat at 3 7 - if the guess was just right, stop it seems as if you should be testing updating_bal against the tolerance: this condition would stop the iteration: -epsilon <= updating_bal <= epsilon this condition indicates guess is too big: updating_bal < -epsilon this condition indicates guess is too small: updating_bal > epsilon you need to be careful where you put things, you might want to look at the order of line 11 and 12-15. print statements help a lot when trying to debug: http://dpaste.com/1007732/ are all thos /12's and *12's an attempt at optimization? early optimization is the root of all evil. keep it simple, get it to work, see if it is fast enough - only then try to determine what part of the code can be optimized
Thank you so very much, bwCA! Your insight really helped. I edited the code and now in the test runs it seems to be working: http://dpaste.com/hold/1008381/
Join our real-time social learning platform and learn together with your friends!