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
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.
copy paste to text editor, replace all ## to nothing :P
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
Thanks, but we still don't know what is the problem with this code. I tested it and it seems to produce good results.
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?
Join our real-time social learning platform and learn together with your friends!