OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter Antonio
  • Start date Start date
A

Antonio

Guest
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.

Code:
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.
<p>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.</p>
<p>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.</p>
<pre><code>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}")

</code></pre>
<p>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.</p>
<p>But what keeps to happen is that it says that dimension us not defined.</p>
 

Latest posts

Top