Quote:
Originally Posted by
PhotonFanatic
And now this! Today my python lessons said:
Code:
# Ask Python to print sqrt(25) on line 3.
print sqrt(5)
Then it says:
Did you see that? Python said: NameError: name 'sqrt' is not defined. Python doesn't know what square roots are—yet.
Its talking about importing modules but instead of "NameError" shouldn't it be "Variableerror"?
Integer is %d rather than %i because like Deepor said: d stands for decimal (base 10). You could also have an integer of base 16 (hexadecimal) or base 8 (octal).
Sqrt() is not a variable, it is a function. Basically python is telling you that it doesn’t know what to do with the number 5, since “sqrt” is undefined at this point.
In C, if you want to use math functions, you type something like “#include <math.h>” which defines math functions so that you can then “call” them inside your program.
I’m guessing Python does something similar. In C, math.h is called a “header” but it sounds like Python calls them “modules.”
Think about how you would handle the square root of 5 if I asked you to compute it by hand (no calculator). What would you do? I’m assuming you would need to look up the method. I would too. (“Newton’s Method” is a popular one)
Python needs to look it up too, in a module.
Just as general advice, I would highly highly suggest taking math courses along side programming. The best programmers are typically very good at math.