pythonclass

Why am I getting 'dimension is not defined' when trying to create a water bottle class in Python?


I am trying to make a class that determines some characteristic of a water bottle: the volume, material, colour and its size (but I want the size to be depended on the volume, so for example if there is only 500ml then the size is small but if there are 2000ml the bottle is large.) and it just doesn't seem to work, it keeps something such as dimension is not defined, and if it doesn't say that it says that the class isn't defined, or size() is not defined. I am really lost.

I am a beginner and it I made up this project so that I can learn how to use class and constructors, so any explanations would be much appreciated.

class WaterBottle:
    def __init__(self, volume, material, colour, dimension):
        self.volume = volume
        self.material = material
        self.colour = colour
        self.dimension = dimension
    volume = int(input("How many ml fit in your bottle?: "))
    def dimension(volume):
        if volume <= 500:
            dimension == "small"
        elif volume < 501 and  evianBottle.volume <= 1000:
            dimension == "med"
        else:
            dimension == "large"
material = input("What material is your bottle?: ")
colour = input("What colour is your water bottle?:")
ml = WaterBottle.volume
dimension = WaterBottle.dimension(ml)
evianBottle = WaterBottle(WaterBottle.volume, material, colour, dimension)

print(f"Your bottle is {dimension} because it hold {ml}, it is made out of {material} and it is {colour}")

I'm expecting the user to input how many ml the bottle has, the material of the bottle and what colour the bottle is. After that I want the program to determine if the bottle is small, medium or large.

But what keeps to happen is that it says that dimension us not defined.


Solution

  • Your code has several problems that I corrected and explained as comments

    I'd recommend you read the documentation and take a tutorial on classes, so you get familiarized with concepts like class instances, attributes, methods, constructors. This way you will better understand my comments

    class WaterBottle:
        # don't pass dimension to the contructor,
        # it will be calculated from the other attributes
        def __init__(self, volume, material, colour):
            self.volume = volume
            self.material = material
            self.colour = colour
            # calculate the dimension by calling the method get_dimension
            self.dimension = self.get_dimension()
    
        # here you need self as the first parameter
        # since the method needs to access class attributes. 
        # You don't need to pass volume
        # since you can access it as it is an attribute
        def get_dimension(self):
            # append self keword to attributes
            if self.volume <= 500:
                # assignment operator is only one single equal sign
                self.dimension = "small"
            # you can chain comparison operators for readability
            elif 500 < self.volume <= 1000:
                self.dimension = "med"
            else:
                self.dimension = "large"
    
    # here you request inputs from the user
    material = input("What material is your bottle?: ")
    colour = input("What colour is your water bottle?:")
    volume = int(input("How many ml fit in your bottle?: "))
    
    # instantiate the class
    evianBottle = WaterBottle(volume, material, colour)
    
    # get the instance attributes and print them
    # split long lines into multiple lines to avoid
    # horizontal scrolling
    print(f"Your bottle is {evianBottle.dimension} \
            because it hold {evianBottle.volume} ml, \
            it is made out of {evianBottle.material} \
            and it is {evianBottle.colour}")