The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Not getting desired display
Discuss Not getting desired display in the Python Programming forum on Dev Shed. Not getting desired display Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 13th, 2012, 10:17 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 7
Time spent in forums: 2 h 17 sec
Reputation Power: 0
|
|
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
|

November 13th, 2012, 10:54 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Location: Texas
Posts: 24
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!
|

November 13th, 2012, 11:01 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 7
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.
|

November 14th, 2012, 01:34 PM
|
 |
Contributing User
|
|
|
|
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!
|

November 14th, 2012, 11:05 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 7
Time spent in forums: 2 h 17 sec
Reputation Power: 0
|
|
|
I was able to get it to work, thanks for the help!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|