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

So, I tried an algorithm to reduce the number of guesses on the PS1, when calculating the monthly payment to pay off within 12 months. When I reduce the margin of error (epsilon) to 0.0, I get a non-terminating sequence. But if I make it 0.00001 or some really small epsilon, it does finish. Is that because of the inaccuracy of floats? http://pastebin.com/afmnWfCq Also, silly question, but I can't figure out how to display dollar figures properly. For example... print 'Monthly payment is: $', round(mon_pmt, 2) http://pastebin.com/4Mdc4qRj ...puts a space between the '$' and the resulting float.

OpenStudy (anonymous):

yep floating point inaccuracy make it almost impossible to get a zero result http://dpaste.com/797164/ http://docs.python.org/library/string.html#format-string-syntax

OpenStudy (lowfriction):

Thanks. I figured. Also, I should have been slightly more specific about the result I was aiming for with the dollar sign. I would prefer to keep the number as a float object rather than a convert to string. The reason is that in some cases, I may need to reuse that float for another calc. Is it possible to remove a part of a string, ie: remove the dollar sign after the print command so that you could convert just the number back to float. Thanks

OpenStudy (anonymous):

New at Python but I will take a stab. In python you don't have to declare what type a variable is so when you wrote pmt_m = 0.0 it took it to be a string(at least as far as I know). Simply define pmt_m type after you assign a number to it. pmt_m = float(pmt_m) tada! Just looking over your code though you may want to change your while statement to <= 12 otherwise it will just do 1month shy of the user's desired number of months, I think :) To remove the space just add a + instead of a , so it wont add on a space. It is wierd working with a language that take common courtesies like that, C++ is all "if you want it there put it there yourself!" print 'Try monthly payment of'+ pmt_m

OpenStudy (lowfriction):

Well, ok, so I assigned a value to pmt_m initially because I used it in an expression before it actually had any other value assigned, but maybe that was unnecessary. But I believe in Python if you use a decimal, it automatically becomes a float. Also, at the start, I add 1 to the months entered by the user, so that should make up for the range issue in the while statement. However, on second look, it is probably better coding to put the +1 in the while statement rather than at the beginning since the variable is used more than once. Finally, print 'Try monthly payment of'+ pmt_m gives an error if pmt_m is a float since you cannot concatenate a float with a string. So, the necessary work around, which is what the sample solution seems to do is convert to a string and then concatenate like you are suggested. Of course, if you want to print out dollar figures during a loop or something, you would have to some how convert back to a float for successive computations, or introduce another variable just for printing perhaps. Thanks for the input!

OpenStudy (anonymous):

"Finally, print 'Try monthly payment of'+ pmt_m gives an error if pmt_m is a float since you cannot concatenate a float with a string. " I suppose it does, I only wanted to display 2 decimal points so I was using something like : pmt_m = "%.2f" % round(pmt_m, 2) so every iteration converted it back to a string then I turned it back into a float etc etc etc, but this was for the first part of the problem set which I neglected to pay attention to in your intitial post you were working on the second part!

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!