I'm trying to figure out why this program runs forever. ir= float(raw_input('enter interest rate: ')) Balance=float(raw_input('enter outstanding Balance: ')) payment=0 while (Balance>0): x=0 payment= payment+10 while (x<12 and Balance>0): interest=round(Balance*ir,2) Balance=round(Balance+interest-payment,2) x+=1 print "%s" %(x) if (Balance>0) and (x>11): Balance= Balance + 12*payment print "payment is %s" %(payment) print "Balance is %s" %(Balance)
use print statement at strategic locations in the code to print out variables - this will help you find the answer.
I tried that to an extent. I inserted a print statement of the Balance and payment right before the second while loop. It seemed that the Balance was never < or = to zero. I'm having trouble getting more information because to get the program to stop for a minute I press debug, but I can't scroll or it starts running again.
One other thing to consider: When the interest rate is entered, if it's say 5%, should it be entered as 5.0 or 0.05? I'm surprised that Balance never seems to go down according to the code.
I'm wondering if maybe there is something wrong with the if statement.
hold down on ctrl-c and it will interrupt the execution
This program will run forever because in your initial step into your first while loop you are only taking away 10 from the balance for your 12 loops. This means that you are only subtracting 120 from your initial balance while you are adding back your 120 at the end as you enter and x=11, you increment x so it is now 12, which meets your 'if' statement conditions over 11. You then are outside of your condition for your second while loop, you then reset 'x' and do it all over again. Also, as @rsmith6559 pointed out, if you input your ir value as an integer, say 5, you are adding to balance your current value * 5. The way this is written, you will never pay off your balance, so while it is realistic, it is not going to allow your program to terminate. :D
@eSpex: each time through the outer loop the payment increases by 10 so at some point the payment should start whittling away at the balance. the problem is in lines 12 and 13.
@bwca That is exactly what I am talking about, although I only specifically described the first pass through. Each time through the second while loop, when x=11, halfway through x is incremented, satisfying the "if" and adding back all that was just taken off. This will never allow the balance to reach a zero. To compound matters the interest rate is multiplying the balance by an integer instead of a percentage. This causes to balance to grow by multiples of the balance rather than by a fraction of the balance.
Join our real-time social learning platform and learn together with your friends!