In Lecture 4, the professor says that even without his explanation, we should be able to understand this code (see below). Unfortunately that is not the case so far. I can't get it to do anything when I run it in IDLE. Can anyone help me figure out how to make sense of it?
I copied this directly from the file "Lec04.py"
def findRoot(pwr, val, epsilon):
"""assumes pwr an int; val, epsilon floats
pwr and epsilon > 0
if it exists,
returns a value within epsilon of val**pwr
otherwise returns None"""
assert type(pwr) == int and type(val) == float\
and type(epsilon) == float
assert pwr > 0 and epsilon > 0
if isEven(pwr) and val < 0:
return None
low = -abs(val)
high = max(abs(val), 1.0)
ans = (high + low)/2.0
while not withinEpsilon(ans**pwr, val, epsilon):
#print 'ans =', ans, 'low =', low, 'high =', high
if ans**pwr < val:
low = ans
else:
high = ans
ans = (high + low)/2.0
return ans

Hey! We 've verified this expert answer for you, click below to unlock the details :)

I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!

Thanks to both of you. I'm troubleshooting this now, with your help.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

## More answers

Looking for something else?

Not the answer you are looking for? Search for more explanations.