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

Help with PS1 Problem 2 please. I am trying to write a code which utilizes bisection search as demonstrated in Lect 3. To do so I plan to use the following condition: while ans*12-X<0: then use the same code from the lecture handout. In my above code, X is a placeholder for the total amount to be paid over the year, but I do not know how to calculate it. So essentially, my question is how do you figure out the total amount to be paid over the course of the year. Or if I'm thinking about the problem the wrong way.

OpenStudy (anonymous):

I'm not sure, but I think you're thinking about it the wrong way. What the program is going to do is try different monthly payment amounts and see what happens. So it will try a certain amount as the monthly payment and then see what happens with that payment amount- is the total amount paid off in less than 12 months? in 12 months exactly? or at the end of 12 months is there still a a balance? Then, depending on what the case is, the program adjusts the monthly payment amount and tries it again. Let me know if this doesn't make sense or if you need more help.

OpenStudy (anonymous):

Thanks for the response! I was thinking about designing the code the wrong way! However, I have tried to adapt the answer provided into a program that uses bisection search (pasted below). This leads me to another question: how can I make it so that the program does not give me a monthly payment that results in a balance far below zero? If you input the example numbers of 320,000 outstanding balance and 0.2 annual interest rate, the result is a monthly payment of 30484.78 and a final balance of -11080.15. The program is correct in that the above monthly payment does not reduce the balance to zero after month 11, but after month 12 reduces it to approx -11k. Here is my code: a=float(raw_input("Enter the outstanding balance on your credit card: ")) b=float(raw_input("Enter the annual credit card interest rate as a decimal: ")) ac=a low=0 high=(a*((1.0+(b/12.0))**12))/12.0 ans=(low+high)/2 bal=a monint=b/12 while bal>0 : ans=(low+high)/2 nummonth=0 bal=a #print(str(ans)) while nummonth < 12 and bal>0: nummonth+=1 interest=monint*bal bal-=ans bal+=interest #print(str(nummonth)) if bal<-0.09: break if bal>0: low=ans balance=round(bal, 2) ansr=round(ans,2) print('RESULT') print('Monthly paymentfor one year:' + str(ansr)) print('Number of months:' +str(nummonth)) print('Balance:'+str(balance))

OpenStudy (anonymous):

Also, for the line "while nummonth < 12 and bal>0:" is there any way to write this as a for statement? I tried to get it to work, but could not figure it out. Thanks!

OpenStudy (turingtest):

a few things here monint=b/12 <-- this is going to result in integer division, so if you are hoping for a decimal result you won't get it while bal>0 : ans=(low+high)/2 nummonth=0 bal=a -here you are repeating the definition bal=a, and then you go on to have an inner loop which has again the condition bal>0, and requires a break statement to exit. This is not very good form. The reason you are getting negative answers is that your program ends when bal<0. That means that the first set of payments that reduce the balance to zero or below are chose. We want the payment that results in almost *exactly* zero balance at the end, to avoid over-paying, so you want to use absolute value: while abs(bal) > 0 however, since you are suing floating point numbers, you need to account for the inherent inaccuracy (meaning you are unlikely to get *exactly* zero as a result, but perhaps some small number close to zero like 0.000241...) hence you don't want to use exactly zero as the bench mark for paying off the debt, but rather while abs(bal) > epsilon where epsilon = e-9, or some other very small number

OpenStudy (turingtest):

in terms of translating your while loop for the months into a "for" statement, you would use something of the following form: for n in range(12) ....

OpenStudy (anonymous):

Thank you so much! One final question then, would the above for statement work only in the context of the changes you mentioned in your first reply? I tried using the code "for z in range (1,13):" to iterate the loop 12 times, but kept getting infinite loops problems (and when I put the print (str(nummonth)) line in the code and attempted to run it kept showing: 1 2 1 2 1 2 etc). Any ideas?

OpenStudy (turingtest):

you put that line of code where exactly? did you swap it for while "nummonth < 12 and bal>0:" ?

OpenStudy (anonymous):

Yes.

OpenStudy (anonymous):

A couple of things to look at in your code; and I'll try not to be too direct in my suggestions. 1) If you're making a money value, make sure you round to the hundredth when making the value. (your guesses and bounds are all money values) 2) You should be careful when you're incrementing iterators to make sure you're evaluating as many iterations as you think you are. ***cough*** nummonths ***cough*** 3) In regards to your infinite loop; look at the exit conditions on both loops and see if you can think of a case that exits the inner loop but not the outer. When that happens, is it ever possible to break out of the outer loop? 4) You also declare a variable you don't appear to use again, it's not contributing to your issues in this case, but but orphan code can confuse matters. I can offer more direct suggestions if you want, but it's very rewarding when it finally does what you want, and you're very close if you haven't already gotten it since your last post.

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!