In: Computer Science
hi i do not know what is wrong with my python code.
this is the class:
class Cuboid: def __init__(self, width, length, height, colour): self.__width = width self.__length = length self.__height = height self.__colour = colour self.surface_area = (2 * (width * length) + 2 * (width * height) + 2 * (length * height)) self.volume = height * length * width def get_width(self): return self.__width def get_length(self): return self.__length def get_height(self): return self.__height def get_colour(self): return self.__colour def set_width(self, width): if isinstance(width, (int, float)): self.__width = width def set_length(self, length): if isinstance(length, (int, float)): self.__length = length def set_height(self, height): if isinstance(height, (int, float)): self.__height = height def set_colour(self, colour): if colour: if colour(" ", "").isalnum(): self.__colour = colour def get_surface_area(self, surface_area): return self.surface_area def get_volume(self, volume): return self.volume def __str__(self): desc = f"The {self.__colour} Cuboid Measurments: \n" \ f"(Width = {self.__width}, Length = {self.__length}, Height = {self.__height})" \ f"Surface Area = {self.surface_area}" \ f"Volume = {self.volume}" return desc
this is the demo code:
from cuboid import Cuboid def main(): cuboid1 = Cuboid("Blue", 10, 13, 54) cuboid2 = Cuboid("Red", 11, 63, 78) print(cuboid1) print(cuboid2) main()
Hi I have resolved the errors.. The code is right but the method signatures were wrong... Like for init you didn't put double underscore.. And a small small syntax errors are there.. Also you have declared the init method with width, length, height, color but in mai you are passing color, height, length, width like that the arguments should be in the same order. Find the below code..
class Cuboid:
def __init__(self, width, length, height, colour):
self.__width = width
self.__length = length
self.__height = height
self.__colour = colour
self.surface_area = (2 * (width * length) + 2 * (width * height) +
2 * (length * height))
self.volume = height * length * width
def get_width(self):
return self.__width
def get_length(self):
return self.__length
def get_height(self):
return self.__height
def get_colour(self):
return self.__colour
def set_width(self, width):
if isinstance(width, (int, float)):
self.__width = width
def set_length(self, length):
if isinstance(length, (int, float)):
self.__length = length
def set_height(self, height):
if isinstance(height, (int, float)):
self.__height = height
def set_colour(self, colour):
if colour:
if colour(" ", "").isalnum():
self.__colour = colour
def get_surface_area(self, surface_area):
return self.surface_area
def get_volume(self, volume):
return self.volume
def _str_(self):
desc=""
desc = desc+"The "+str(self.__colour)+" Cuboid Measurments:"
+"Width = "+str(self.__width) +", Length = "+str(self.__length)+",
Height = "+str(self.__height) +" , Surface Area =
"+str(self.surface_area) +" , Volume = "+str(self.volume)
return desc
def main():
cuboid1 = Cuboid(10,13,54,"Blue")
cuboid2 = Cuboid(11,63,78,"Red")
print(cuboid1.get_colour())
print(cuboid2.get_colour())
print(cuboid1.get_volume(900))
print(cuboid2.get_surface_area(89))
print(cuboid1._str_())
main()
Output :