For the first square root program in lecture 3, when I try to execute the code on my machine (using copy/paste) I get a typeerror:number is not a function. Why is this?
You're code is wrong, surprise. Without seeing the code, it's impossible to tell exactly what's wrong.
x = 25 epsilon = 0.01 numGuesses = 0 ans = 0.0 while abs(ans**2 - x) >= epsilon and ans <= x: ans += 0.00001 numGuesses += 1 print 'numGuesses =', numGuesses if abs(ans**2 - x) >= epsilon: print 'Failed on square root of', x else: print ans, 'is close to square root of', x
I typed out your code and ran it. It ran fine, first time. Talk about finding out nothing. Since Python uses indentation to "block" the code, I'd go through the code and check the spacing, only because you copied and pasted and I typed it out. It also ran in Python 3 after I changed the print statements, which is expected. FWIW, I did this with Python 2.5.1, 2.6.4 and 3.1.2.
I copied and pasted your code exactly and it worked. I have no idea. Is this the exact code you have, character for character?
Thanks for your help. I think it was because I was using the wrong version of python (print statements).
Join our real-time social learning platform and learn together with your friends!