I'm doing Problem set 1 and am on problem 3. I have a code that makes sense to me, only I'm getting an error message from this line of code: "for guess in range(low, high, .01):" Can someone please help me figure out how to properly use the range function and/or any alternatives?
My code: balance = float(raw_input('What is the outstanding balance?')) annual_rate = float(raw_input('In decimal form, what is the annual interest rate?')) monthly_rate = float(annual_rate/12) month = 0 new_balance = balance total_paid = 0 low = balance/12 high = (balance*(1+monthly_rate)**12)/12 guess = (low+high)/2.0 for guess in range(low, high, .01): new_balance = round(new_balance * (1 + monthly_rate) - guess, 2) total_paid += guess month += 1 guess += .01 if month == 12 and new_balance > 0: low = guess high = high guess = round((low+high)/2.0, 2) elif new_balance <= 0: break print 'Monthly payment to pay off debt in 1 year: ', guess print 'Number of months needed: ', month print 'Balance: ', new_balance
TypeError: range() integer end argument expected, got float. <--- You can only use integers as 3rd argument to range, read the error messages :). The real issue is how you seem to be mixing bisection srch with guess-check. You are using two different ways to take guesses, one with range and the other with (low+high)/2, try to get rid of the for loop. Use while and take your guesses using bisection only.
Thanks for the response halr2s. Here's my second crack at it. The problem with this code is that I don't know how to exclude guesses that are greater than the actual answer. So if the minimum payment is supposed to be 2,970, but my code tries 3,015, it will view that guess as the answer. Any ideas? balance = float(raw_input('What is the outstanding balance?')) annual_rate = float(raw_input('In decimal form, what is the annual interest rate?')) monthly_rate = float(annual_rate/12) month = 0 new_balance = balance total_paid = 0 low = balance/12 high = (balance*(1+monthly_rate)**12)/12 guess = (low+high)/2.0 while (month<=12) and new_balance > 0: guess = round((low+high)/2, 2) new_balance = round(new_balance * (1 + monthly_rate) - guess, 2) total_paid = total_paid + guess month += 1 print 'guess: ', guess print 'new balance: ', new_balance print 'total paid: ', total_paid print 'month: ', month if new_balance <= 0: ##print 'Monthly payment to pay off debt in 1 year: ', payment ##print 'Number of months needed: ', month ##print 'Balance: ', new_balance break elif(new_balance>0) and month == 12: month = 0 new_balance = balance total_paid = 0 low = guess high = high guess = round((low+high)/2.0, 2) ##print new_balance*-1 ##for guess in range(low, high, 1): ## new_balance = round(new_balance * (1 + monthly_rate) - guess, 2) ## total_paid += guess ## month += 1 ## guess += .01 ## if month == 12 and new_balance > 0: ## low = guess ## high = high ## guess = round((low+high)/2.0, 2) ## elif new_balance <= 0: ## break print 'Monthly payment to pay off debt in 1 year: ', guess print 'Number of months needed: ', month print 'Balance: ', new_balance
You need to find whether your guess is "good enough", testing for balance >0 won't do it, since every min payment above the required limit will cause it to be less than 0. Typically you'll take an "epsilon", say one cent (.01) and test if for one particular guess, the absolute value of your balance at the end is less than your "epsilon", something like abs(rem_balance) < epsilon btw i apologize for any typos etc, im learning english too
how bout using a code pasting site to post your code?? http://dpaste.com http://pastebin.com http://codepad.org .............
Well now I'm at a total loss...here's my third attempt at it: http://pastebin.com/ki1sixCB It seemed to find the answer, but I couldn't get it to exit it's loop. So I changed all my 0.01's to .0's. Then something really weird happened. Python is telling me I have invalid syntax for almost every line of code. Assigning guess to a value? error. using a while statement? error. etc. etc. etc. All the commands that had worked a second ago were now invalid. What happened?
you are missing a paren in line 8
Join our real-time social learning platform and learn together with your friends!