#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Exclamation Trying to get base class object to display (Please help!)


    This is my output and below is my code. How do I get the color and filled property to display rather than this
    <__main__.GeometricObject object at 0x000000000319FEF0> at the end of my output?
    Code:
    Enter a value for side 1: 3
    Now enter a value for side 2: 4
    Now enter a value for side 3: 5
    Enter the color of the triangle: blue
    Enter true(filled) or false(not filled) for the triangle being filled: true
    
    
    
    A Triangle:  Triangle: side 1 = 3.0 side 2 = 4.0 side 3 = 5.0 <__main__.GeometricObject object at 0x000000000319FEF0>
    The area is:  6.0
    The perimeter is:  12
    Code:
    from math import *
    class GeometricObject():
        def __init__(self, color = "green", filled = True):
            self.color = color
            self.filled = filled
            
            def getColor(self):
                return self.color
    
            def setColor(self, color):
                self.color = color
                
            def getFilled(self):
                return self.filled
    
            def setFilled(self, filled):
                self.filled = filled
                
            def __str__(self):
                return "color: " + self.__color + \
                       " and filled: " + str(self.__filled)
    
    
    class Triangle(GeometricObject):
        def __init__(self, side1 = 1, side2 = 1, side3 = 1):
            super().__init__()
            self.side1 = float(side1)
            self.side2 = float(side2)
            self.side3 = float(side3)
    
        def getArea(self):
            return self.area
        
        def setArea(self, side1, side2, side3):
            s = (side1 + side2 + side3)/2
            area = sqrt(s*(s-side1)*(s-side2)*(s-side3))
            self.area = area
            
        def getPerimeter(self):
            return self.perimeter
        
        def setPerimeter(self, side1, side2, side3):
            perimeter = side1 + side2 + side3
            self.perimeter = perimeter
    
        def __str__(self):
            return "Triangle: " + "side 1 = " + str(self.side1)+ \
                   " " + "side 2 = " + str(self.side2) + " " + \
                    "side 3 = " + str(self.side3)
    
    def main():
        side1 = eval(input("Enter a value for side 1: "))
        side2 = eval(input("Now enter a value for side 2: "))
        side3 = eval(input("Now enter a value for side 3: "))
        color = input("Enter the color of the triangle: ")
        filled = input("Enter true(filled) or false(not filled) " + \
                       "for the triangle being filled: ")
        
        go = GeometricObject(color, filled)
        tri = Triangle(side1, side2, side3)
        tri.setArea(side1, side2, side3)
        tri.setPerimeter(side1, side2, side3)
    
        print("\n\n")
        print("A Triangle: ", tri.__str__(), go.__str__())
        print("The area is: ", tri.getArea())
        print("The perimeter is: ", tri.getPerimeter())
    
    main()
    Last edited by crazyjdog; November 13th, 2012 at 10:46 PM. Reason: more descriptive title
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Texas
    Posts
    24
    Rep Power
    0
    Did not do a complete study on your code, but did notice that you use true and false instead of True and False.

    First letter has to be capitalized.

    EDIT: Tried to run the program, and can see that's not the issue you're asking about. Sorry!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    I tried with "True" instead of "true" and I still get this
    <__main__.GeometricObject object at 0x000000000319FEF0> as the second part of my output.
    The thing is I don't know what is missing to make "color: blue and filled: True" to display instead of what is being displayed.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,703
    Rep Power
    480
    The problem is, and it took me a while to see, is that your indentation is screwy. You defined a batch of functions within the GeometricObject.__init__ method scope.
    Code:
    from math import *
    class GeometricObject():
        def __init__(self, color = "green", filled = True):
            self.color = color
            self.filled = filled
            
            def getColor(self):
                return self.color
            #...
    What is __filled ?
    What is __color ?

    Instead of obj.__str__() use str(obj) . That's the main point of the methods with special names.

    What if I provide edge lengths of 1 1 and 3 ?
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    I was able to get it to work, thanks for the help!

IMN logo majestic logo threadwatch logo seochat tools logo