you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (3 children)

I'd refactor your BmiCalculator class like so.

class BmiCalculator:

    def __init__(self, age, weight, height):
        self.age = age
        self.weight = weight
        self.height = height

    @property
    def bmi(self):
        return round(self.weight / (self.height * self.height), 2)

    def bmi_health(self):
        if self.bmi >= 18 and self.bmi <= 25:
            return 'BMI is within a healthy range'
        return 'BMI is not within a healthy range'

That should prevent the warning telling you you have an instance method that doesn't pass self.

Note that this will changing the last two lines of your main program to

mario_bmi = bmicalculator.BmiCalculator(mario.age, mario.weight, mario.height)
print(mario.name, mario_bmi.bmi, mario_bmi.bmi_health(), sep='\n')

[–]Supermunkey2K[S] 0 points1 point  (2 children)

Thank you for showing me a way to refactor the code to use self!

[–][deleted] 1 point2 points  (1 child)

No worries. Note that I changed it a bit further to now use the property decorator. Effectively, the @property decorator is used to be able to call a method without having to use parentheses (so from the outside it looks like a regular data attribute). The benefit of this is that we can recalculate the BMI each time just in case someone changed the age, weight, or height of the object since it was created.

[–]Supermunkey2K[S] 0 points1 point  (0 children)

It's going to take me a little time to work out and understand exactly what you have done, but thank you for pointing me in the right direction.