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

i am on lecture six and using python 2.7.2 whats wrong with my copied code? ##def sqrtbi(x, z): ## assert x>=0, 'x must be non-negitive, not' + str(x) ## assert z > 0, 'espilon must be positive, not' + str(z) ## low = 0 ## high = max(x, 1.0) ## guess = (low + high)/2.0 ## ctr = 1 ## while abs(guess**2 - x) > z and ctr <= 100: ## #print 'low:', low, 'high:', high, 'guess:', guess ## if guess**2 < x: ## low = guess ## else: ## high = guess ## guess = (low + high)/2.0 ## ctr += 1 ## assert ctr <= 100, 'Ineration

OpenStudy (maitre_kaio):

Please give code that is easy to try ! This code is fully commented, and we have to reformat it. Plus it seems to be incomplete (end missing). Use a pastebin and try to ease the job of those wanting to help... And explain what is the problem with the code, according to you.

OpenStudy (anonymous):

copy paste to text editor, replace all ## to nothing :P

OpenStudy (anonymous):

def sqrtbi(x, z): assert x>=0, 'x must be non-negitive, not' + str(x) assert z > 0, 'espilon must be positive, not' + str(z) low = 0 high = max(x, 1.0) guess = (low + high)/2.0 ctr = 1 while abs(guess**2 - x) > z and ctr <= 100: #print 'low:', low, 'high:', high, 'guess:', guess if guess**2 < x: low = guess else: high = guess guess = (low + high)/2.0 ctr += 1 assert ctr <= 100, 'Ineration count exceeded' print 'Bi method. Num. Interactios', ctr, 'Estimation', guess return guess

OpenStudy (maitre_kaio):

Thanks, but we still don't know what is the problem with this code. I tested it and it seems to produce good results.

OpenStudy (anonymous):

when i test it, the code spits out obserd awnsers such as sqrtbi(9, 0.0001) = 2.25... instead of 3, how can i count on it to find the square root of any given number?

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!