# class.py
# tpm
# A program to calculate the volume and surface area of a sphere from its
# radius given as input.

from math import *

class Sphere:

    def _init_(self, radius):
        self.radius = radius

    def getRadius(self):
        return self.radius

    def surfaceArea(self):
        self.area = 4.0 * pi * self.radius ** 2
        return self.area

    def volume(self):
        self.volume = 4.0 / 3.0 * pi * self.radius ** 3
        return self.volume

    def main():
        r = input('Enter the radius of the sphere ')
        s = Sphere(r)
        print 'The surface area of the sphere is: ', s.surfaceArea()
        print 'The volume of the sphere is: ', s.volume()

    main()

Recommended Answers

All 2 Replies

Very nice effort, just a a few corrections needed to make this work.

# class.py
# tpm
# A program to calculate the volume and surface area of a sphere from its
# radius given as input.

from math import *   # for pi

class Sphere:

    def __init__(self, radius):   # use double underline on both endes of init
        self.radius = radius

    def getRadius(self):
        return self.radius

    def surfaceArea(self):
        self.area = 4.0 * pi * self.radius ** 2
        return self.area

    def volume(self):
        self.volume = 4.0 / 3.0 * pi * self.radius ** 3
        return self.volume


# main is not part of class, so indent properly
def main():
    r = input('Enter the radius of the sphere ')
    s = Sphere(r)
    print 'The surface area of the sphere is: ', s.surfaceArea()
    print 'The volume of the sphere is: ', s.volume()

main()

I commented on the problems, hope you understand them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.