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

Hi pretty new to programming. I am doing problem set 1c and my program will not get out of the loop. I have spent hours on it. Can anyone tell me what I am doing wrong? Here is my code: The aim is to find minimum payments to pay off a debt in 12 months using the bisection code. Thank you balance = float(raw_input('Whats your credit card debt?: ')) annualIntr=float(raw_input('Annual Interest?: ')) low = balance/12 hi = (balance*(1+(annualIntr/12))*12.0)/12.0 newBalance = balance while balance > 0: payment = (hi+low)/2.0 for month in range(1,13): newBalance = round((newBalance*(1 + (annualIntr/12))-(payment)),2) if newBalance <= 0: break if newBalance == 0.0: balance = newBalance elif newBalance > 0.0: newbalance = balance low = payment else: newBalance = balance hi = payment print 'RESULTS' print 'Monthly Payment: '+str(round(low,2)) print 'Number of Months to pay: '+str(month) print 'Your balance: '+str(round(balance,2))

OpenStudy (anonymous):

I didn't check for the infinite loop but you have four cases in your code. The last else, will never be executed. Cause if newBalance is less than zero, it will break out of the for. Or maybe I didn't get the indentation properly. Correct me if I'm wrong.

OpenStudy (anonymous):

Hi Ray & thanks for ur reply. I made a mistake when editing the indentation during submission. I corrected the indentation.

OpenStudy (anonymous):

You might try posting your code in something like pastebin or dpaste to make it easier to read... Like Ray13z said, your last else statement will never be run nor will 'elif balance == 0'. These conditions are skipped because they are in 'While balance > 0:...' loop.

OpenStudy (anonymous):

Your condition "while balance > 0" in your main loop is always true since that's the input from the user. There is no line in the code that changes the value of balance (so balance is constant). Hence, if the input of the user is positive, the code in your while loop is endlessly run. To solve this, you simply change the condition of your while loop. I won't tell you what that condition is. You should be the one to figure it out. The clue is that you do not have to introduce another variable and you have to test if something is "close enough" to zero. I hope this helps :)

OpenStudy (anonymous):

Thanks guys really appreciate your input. I made an error in the while loop. The elif has newbalance = balance instead of newBalance =balance. So my loop only adjusted the 'low' and hence overshot the answer such that newBalance == 0.0 could never be true. I hope I am making sense. Thanks again guys.

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!