Python Math libraries - How to?

aguirre.adolfo at aguirre.adolfo at
Tue Apr 29 04:07:52 CEST 2008

Hi, I am a very newbie who would very much appreciate some hints.

Python 2.52. on Windows XP for now. Soon on Ubuntu 8

I am teaching myself Python following free tutorials. I can solve
problems using arithmetic, but when I try to upgrade the programs
using math libraries nothing seems to work. I downloaded a 2002
tutorial from Zelle "An Introduction to Computer Science" where he
uses a "import math" statement to calculate a square root. I tried the
"pi" library function but it didn´t work. I tried using def Pi()  it
did not work either. I am yet to find a tutorial that explains how to
declare (or initialize) and pass numbers to the functions such as
"cos(x)" and the pi which does not have a variable in it. Is just a

Here is the arithmetic program I made that it worked before I added
the "import math" line.  I  erased the constant p = 3.1416 and added
the "i" for the library function "pi" in the algorithms. But I get an
error message not recognizing "pi"
# A program to compute the volume and surface area of a sphere
import math

def main():

    print "This program calculates the volume and surface area of a
    r = input("Please enter the radious: ")
    r3 = r*r*r
    volume = 4/3*pi*r3
    r2 = r*r
    surface = 4*pi*r2
    print "The Volume is", volume, " Cubic centimeters"
    print "The Surface area is", surface, " square centimeters"


*** Error message *************

Traceback (most recent call last):
  File "C:/Python25/z - MIS PROGRAMAS/", line 20, in
  File "C:/Python25/z - MIS PROGRAMAS/", line 13, in main
    volume = 4/3*pi*r3
NameError: global name 'pi' is not defined

More information about the Python-list mailing list