0

The goal was to design a class named "Triangle" , that extends from the class "GeometricObject" , the overall code works fine and has no errors but the problem is that the "filled" value won't change and also , I'm not able to set the initial values for the parent class attributes using the child's class constructor

I tried to set the value for filling to = 0 , but it still prints the value = True , instead of the value = False

and for the constructor issue , I tried triangle1 = Triangle(color , filling , s1 , s2 , s3) to initialize the attributes from both the parent and child classes but it resulted in an error , so instead I changed it as below :

import math

class GeometricObject:
    def __init__(self , color = "green" , filled = None):
        self.__color = color
        self.__filled = filled

    def getColor (self):
        return self.__color

    def setColor (self , color ):
        self.__color = color

    def setFilled (self , filled):
        self.__filled = filled
     
    def isFilled (self):
        return self.__filled

    def __str__ (self):
        return "color:  " + self.__color + \
               "and filled:  " + str(self.__filled)

#child class:

class Triangle (GeometricObject):
    def __init__(self , side1 = 1.0 , side2 = 1.0 , side3 = 1.0):
        super().__init__()
        self.side1 = side1
        self.side2 = side2
        self.side3 = side3
    
    #setters:

    def setSide1 (self , side1):
        self.side1 = side1

    def setSide2 (self , side1):
        self.side2 = side2

    def setSide3 (self , side3):
        self.side3 = side3

    #getters:

    def getArea (self):
        s = (self.side1 + self.side2 + self.side3)/2
        area = math.sqrt(s * (s  - self.side1) * (s - self.side2) * (s - self.side3))
        return area 

    def getPerimeter (self):
        return (self.side1 + self.side2 + self.side3)

    #__str__ ()method 
    def __str__(self):
        return super().__str__() + " Triangle: side1 =  " + str(self.side1) + " side2 =  " + str(self.side2) + " side3 =  " + str(self.side3)
        
s1 , s2 , s3 = eval(input("please enter the three sides of the triangle seperated by commas: "))
color = input("please enter the color of this triangle: ")
filling = bool(input("enter 1 for a filled triangle and 0 for an empty one: "))

triangle1 = Triangle(s1 , s2 , s3)
triangle1.setColor(color)
triangle1.setFilled(filling)

#print(triangle1)

print("The area of this triangle is: " , round(triangle1.getArea()) , "m^2" )
print("The perimeter of this triangle is: " , triangle1.getPerimeter() , "m")
print("The color of this triangle is: " , triangle1.getColor())
print("The filling value: " , triangle1.isFilled())
Barmar
  • 741,623
  • 53
  • 500
  • 612
dareen42
  • 3
  • 1
  • 3
    `bool(0)` is False, but `bool('0')` is True, because `'0'` is a non-empty string. – user2357112 Feb 22 '23 at 19:17
  • Use `bool(int(input(...)))` – Barmar Feb 22 '23 at 20:35
  • Don't use `eval(input(...))`. To read a list of numbers, use `map(float, input().split(','))` – Barmar Feb 22 '23 at 20:36
  • This is an example of how *not* to define an inheritance hierarchy. It's one thing for `GeometricObject.__init__` to have default argument argument values; quite another to make it impossible for the caller of `Triangle.__init__` to override them. – chepner Jun 08 '23 at 19:19
  • (Never mind the boilerplate getters and setters that could be discarded by simply working with the attributes directly.) – chepner Jun 08 '23 at 19:19

1 Answers1

0

The problem is that you are calling bool on the return value of input: the input call will always return a string, and despite what one might guess seeing int(input()) all around the web, this is not a "casting" in Python, nor an indicator that input shuld returna value of the type int: it will simply call int with the string provided by input.

While the int constructor will change a string containing the digit 0 "0" to the value 0, the bool constructor is quite well defined in Python: the only things with a False value are any number 0, None, False itself or any empty container. But "0" is one string with one character. It is truth value is True - bool does not check what is inside containers, only if they are empty or not.

If you want to keep your program as it is, just convert your input value to an integer, and no to a bool - 0 is already falsy, and there is no need to further convert it to "bool":

filling = int(input("enter 1 for a filled triangle and 0 for an empty one: "))
jsbueno
  • 99,910
  • 10
  • 151
  • 209