0

# 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()
2
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by butterflyTee
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.