Hello everyone, my problem that I'm have is that I have to get the volume and the surface area of a sphere using a class. I believe I was able to do that part of the problem correctly. The issue I'm having is getting that information to print from the class.

Basically all I need to do now is to get volume and surface area to print.

Here is the code I'm coming up with:

import string
import math
#import systemexit thingy
class solidsphere:

    def __int__(self,radius):
        self.radius2 = float(radius2)

    def getRadius(self):
        return self.radius2

    def surfaceArea(self):
        area = 4*math.pi*self.radius2**2
        self.area = area
        print self.area
        return self.area
    def volume(self):
        volume = (4/3)*math.pi*self.radius2**3
        self.volume = volume
        print self.volume
        return self.volume

    #def radiusInput():
    #    radius = input("Enter in the radius of the sphere: ")
    #    return (radius)

#If radius is negative return error message
def radiuscheckuserinput(radius2):
    if radius2 <= 0:
        print "See next line"
        print "See next line"
        print "See next line"
        print "See next line"
        print "Error"
        print "You did something wrong"
        print "Do you feel bad about what you did?"
        print "Well you should!"
        print "You did not enter a valid radius"
        print "Radius must be a positive number not a negative"
    if radius2>= 0:
        print "acceptable value"

#def sphereoutput():
#   return solidsphere(Radius2, volume, surfaceArea)
#    print s.radius2()
#    print s.area()
#    print s.volume()

def main():
    radius = input("Enter in the radius of the sphere: ")
    #print "The surface area of the sphere is", surfaceArea()
    #print "The volume of the sphere is", volume()
    #radius = radiusInput()
    #print radius
    radius2 = radiuscheckuserinput(radius)
    print radius2
    #area = surfaceArea(self)
    #print self.area
    #volume = volume(self)
    #print self.volume

thanks for your help. I feel like the solution is just sitting right infront of me but I'm just not recognizing it!

10 Years
Discussion Span
Last Post by imamyth

Anyone? I just need help on getting to print surface area and volume.


From the code you have posted, it looks like there may be some confusion as to how and where variables are accessible...

In any case, if you just want to print a value from a class, consider one that looks like this...

class sphere:
    def __init__(self, radius):
        if radius <= 0: 
            print "Wrong!"
            self.radius = radius

    def area(self):
        a = # result of formula using self.radius instance variable
        print a

in order to have this class print the area you have to create a new instance of your class and then call the area method, like this:

newsphere = sphere(input('Radius: '))

you should be presented with the value that was assigned to a in the area method.

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.