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

I think I have ps1b figured out, but in line 26 if I put math.log(iter) python freaks out. It seems to work with just log(iter), but I thought you needed the math. in front of the log to call the function from the module... Can someone explain how this works/why it doesn't? I'm not actually sure I got it right. http://codepad.org/dCw5DO15

OpenStudy (anonymous):

I guess that the -- "from math import *" makes the functions in math seem local. If you use the incantation "import math" then use would use math.log().

OpenStudy (anonymous):

That is correct. And, in general, you should always use "import math". If you do "from x import *" with multiple packages that have functions with the same name, you'll lose access to one of them. Also, it's better to require yourself to type "math.log(x)" or "string.find('abc', 'b')" because then you'll have a reminder of where that function came from (and anyone else who has to read your code). I can't actually think of an example where you'd want "from x import *" so I'm going to change my advice to say ALWAYS use "import x".

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!