Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 13th, 2012, 10:17 PM
crazyjdog crazyjdog is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 crazyjdog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 17 sec
Reputation 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

Reply With Quote
  #2  
Old November 13th, 2012, 10:54 PM
KING-OLE KING-OLE is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Location: Texas
Posts: 24 KING-OLE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 54 m 45 sec
Reputation 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!

Reply With Quote
  #3  
Old November 13th, 2012, 11:01 PM
crazyjdog crazyjdog is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 crazyjdog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 17 sec
Reputation 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.

Reply With Quote
  #4  
Old November 14th, 2012, 01:34 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
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!

Reply With Quote
  #5  
Old November 14th, 2012, 11:05 PM
crazyjdog crazyjdog is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 crazyjdog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 17 sec
Reputation Power: 0
I was able to get it to work, thanks for the help!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Not getting desired display

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap