Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 7 Online
OpenStudy (anonymous):

working on problem set 1 question 2. I am not sure why my code isn't working, well, it is working but it is printing out for every month, and it is not printing the correct thing. Any help is much appreciated.

OpenStudy (anonymous):

outbal=float(raw_input('Enter the outstanding balance on your credit card: ')) annin=float(raw_input('Enter the annual interest rate as a decimal: ')) monin=annin/12 upbal=outbal mmp=10 while (upbal>=0): for month in range(1,13): upbal=outbal*(1+monin)-mmp if upbal<=0: print 'Monthly payment to pay off debt in one year: ', mmp print 'Number of months needed: ', month else: mmp = mmp + 10 outbal = upbal

OpenStudy (anonymous):

Insert a break statement in the 'if' block under the 'for loop' see if that solves your problem

OpenStudy (anonymous):

i got the wrong answer. i got that i takes a monthly payment of $170 and 5 months rather than a monthly payment of $120 and 11 months. that fixed the fact that it was printing every month though

OpenStudy (anonymous):

instead of 'monin=annin/12' do 'monin=annin/12.0' (never divide by an integer in python!) and use the round function to round up the intermediate result to two decimal places round(upbal=outbal*(1+monin)-mmp,2) That should work!

OpenStudy (anonymous):

ok. I did that, now after i enter my information it wont print anything. it just keeps the program running without doing anything. here is the edited program outbal=float(raw_input('Enter the outstanding balance on your credit card: ')) annin=float(raw_input('Enter the annual interest rate as a decimal: ')) monin=annin/12.0 upbal=outbal mmp=10 while (upbal>=0): for month in range(1,13): round(upbal==outbal*(1+monin)-mmp,2) if upbal<=0: print 'Monthly payment to pay off debt in one year: ', mmp print 'Number of months needed: ', month break else: mmp = mmp + 10 outbal = upbal thank you for all your help btw

OpenStudy (anonymous):

oh, and i have tried it with and without the break.

OpenStudy (anonymous):

put the print statements outside the for loop and while loop can solve the very initial problem (ie printing out every month... use simply break for the first if statement

OpenStudy (anonymous):

So i know that the problem is somewhere in one of the loops. For some reason, its not understanding the mmp=mmp+10. I think that may be the issue at least. It is getting caught in one of the loops and i don't think upbal is ever <=0 so the program won't ever terminate. outbal=float(raw_input('Enter the outstanding balance on your credit card: ')) annin=float(raw_input('Enter the annual interest rate as a decimal: ')) monin=annin/12.0 upbal=outbal mmp=10 while (upbal>=0): for month in range(1,13): round(upbal==outbal*(1+monin)-mmp,2) if upbal<=0: print 'Monthly payment to pay off debt in one year: ', mmp print 'Number of months needed: ', month else: mmp = mmp + 10 outbal = upbal

OpenStudy (anonymous):

you've written round(upbal==outbal*(1+monin)-mmp,2) == is a relational it gives a boolean output namely True and False. Use an assignment (=) and see what happens Use break statement in the for and the while loop.. Generally you should use print statements to see why your code is not working.put a print statement in the while loop , the for loop and outside the loop. See what output you're getting that would give you valuable hints as to why your program is not working

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!