At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
yea i do.
did you have any problems with defining functions in lecture 4?
nope i did it!
I'm using code swiped from the pdf, but I keep getting NameError: name 'sqrt' not defined. I can't figure out what I've not done.
first "import math"
I think that may be my problem! do I do it thru the menu?
no,just type 'import math' as first line of your program
could you show me a copy of the code?
I tried what you said, but it didn't work... :(
import math x = 16 ans = 0 if x >= 0: while ans*ans < x: ans = ans + 1 print 'ans =', ans if ans*ans != x: print x, 'is not a perfect square' else: print ans else: print x, 'is a negative number' def sqrt(x): ans = 0 if x >= 0: while ans*ans < x: ans = ans + 1 if ans*ans != x: print x, 'is not a perfect square' return None else: return ans else: print x, 'is a negative number' return None
thanks for ur help. It's not working out for me. I think I need a small break. Maybe when I get back it will look better.
Traceback (most recent call last): File "
", line 1, in
NameError: name 'sqrt' is not defined
Funny enough, the code works, it's just making the function sqrt(x) that doesn't work. oh well.
my bad,i didn't checked the code just copy paste :(
sqrt() is a function/method of the math module - it is in the math 'namespace' to access methods of an imported module you have to use module.method read 9.1 and 9.2 of the Tutorial - I had to refer to it many times on and off as i learned more stuff. The documentation should also be installed on your computer - i find that version easier to use (F1 from within Idle). http://docs.python.org/tutorial/classes.html#a-word-about-names-and-objects