Noah Dukehart
Noah Dukehart

Reputation: 31

Working with classes in python

I am working on a simple cuboid class program in python and I am very new to it all so I have a few questions about the first version of my code:

from math import *

class Cuboid:
    def __init__(self,length,width,height):
        self.length=length
        self.width=width
        self.height=height
        self.LSA=0
        self.SA=0
        self.volume=0
    def getDimensions(self):
        return self.length
        return self.width
        return self.height

    def LateralSurfaceArea(self):
        LSA=2*((self.length*self.height)+(self.width*self.height))
        return LSA

    def SurfaceArea(self):
        SA=2*((self.length*self.width)+(self.width*self.height)+(self.height*self.length))
        return SA

    def Volume(self):
        Volume=self.length*self.width*self.height
        return volume

My first question, is this the proper way to set up a class and initialize it? second question, are there any glowing errors in this part? I am working out of a textbook teaching myself and it has no examples. Finally my main:

from cuboid import *

def main():
    cb = Cuboid(3, 4, 5)
    l, w, h = cb.getDimensions()
    print("The length of the cuboid is", l)
    print("The width of the cuboid is", w)
    print("The height of the cuboid is", h)
    print("lateral surface area=", cb.LateralSurfaceArea())
    print("surface area=", cb.SurfaceArea())
    print("volume=", cb.Volume())

main()

when I run my main function I get the following error:

l, w, h = cb.getDimensions()
TypeError: 'int' object is not iterable

Does anyone have any idea why this error is coming up and anyway I can get around it? sorry I know I'm only supposed to ask a specific question but for the sake of learning properly id rather make sure I'm going in the right direction as classes are new to me.

Upvotes: 3

Views: 1144

Answers (2)

Hugh Bothwell
Hugh Bothwell

Reputation: 56694

A slightly cleaned-up version:

class Cuboid:
    def __init__(self, length, width, height):
        self.length = length
        self.width  = width
        self.height = height

    @property
    def lateral_surface_area(self):
        front_area = self.length * self.height
        side_area  = self.width  * self.height
        return 2 * (front_area + side_area)

    @property
    def surface_area(self):
        top_area   = self.length * self.width
        front_area = self.length * self.height
        side_area  = self.width  * self.height
        return 2 * (top_area + front_area + side_area)

    @property
    def volume(self):
        return self.length * self.width * self.height

def main():
    cube = Cuboid(3, 4, 5)
    print("The length of the cuboid is", cube.length)
    print("The width of the cuboid is",  cube.width )
    print("The height of the cuboid is", cube.height)
    print("Lateral surface area =", cube.lateral_surface_area)
    print("Surface area =", cube.surface_area)
    print("Volume =", cube.volume)

if __name__=="__main__":
    main()

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245489

When returning multiple values, you don't use multiple return statements. Instead, you return a tuple:

return (self.length, self.width, self.height)

In your case, only the first return statement gets executed thus passing a single int to the caller. It then tries to unpack that value into the three variables you specified by iterating over it. The single value that was returned isn't iterable, hence the error.

Upvotes: 3

Related Questions