[Tutor] Use of sqrt() from math module

Michael H. Goldwasser goldwamh at slu.edu
Sat Dec 1 16:19:54 CET 2007


Matt,

After using "import math" you will need to use the qualified name
math.sqrt(blah) to call the square root function.   That explains the
NameError when trying to use the unqualified name, sqrt.

As to your first message, the ValueError that you are reporting with
the usage math.sqrt is likely due to an attempt to take the square
root of a negative number (presumably because your (ypos - 384 * 160)
factor is negative.

With regard,
Michael


On Saturday December 1, 2007, Matt Smith wrote: 

>    Matt Smith wrote:
>    > import sys, pygame, math
>    > 
>    > ...
>    > 
>    >     if ypos >= 384 and velocity > 0:
>    >         impactvel = sqrt(velocity ** 2 + (2 * gravity * (ypos - 384 * 
>    > 160)))
>    > 
>    > ...
>    > 
>    > 
>    > Traceback (most recent call last):
>    >   File "<stdin>", line 32, in <module>
>    > ValueError: math domain error
>    
>    Apologies, the actual error I get when I run the code above is:
>    
>    Traceback (most recent call last):
>       File "<stdin>", line 31, in <module>
>    NameError: name 'sqrt' is not defined
>    
>    The error I quoted first was when I tried to call 'math.sqrt(x)'
>    
>    Matt
>    
>    _______________________________________________
>    Tutor maillist  -  Tutor at python.org
>    http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list