0

so I have a project that is pretty straight forward. It's a simple BMI calculator and should just simply print,

"The BMI for (name) is (BMI), (status, like if they're underweight, overweight etc.)

name = str(input("What is your name? "))
age = int(input("What is your age? "))
weight_float = float(input("What is your weight in pounds? "))
height_float = float(input("What is your height in inches? "))

Pounds2Kilogram = weight_float * 0.453592
Inches2Meter = height_float * 0.0254

weight = Pounds2Kilogram
height = Inches2Meter


class calcBMI:

    def __init__(self, name, age, weight, height):
        self.__name = name
        self.__age = age
        self.__weight = weight
        self.__height = height

    def getBMI(self):
        return self.__weight / (self.__height **2)

    def getStatus(self):
        if BMI < 18.5:
            self.__getStatus = "Underweight"
        elif 18.5 < BMI < 24.9:
            self.__getStatus = "Normal"
        elif 25.0 < BMI < 29.9:
            self.__getStatus = "Overweight"
        elif BMI > 30:
            self.__getStatus = "Obese"

    def getName(self):
        return self.__name

    def getAge(self):
        return self.__age

    def getWeight(self):
        return self.__weight

    def getHeight(self):
        return self.__height

    def __str__(self):
        return "The BMI for", + self.__name(), + "is", + self.__getBMI()

    def PrintBMI(self):
        print(self.__str__())

a = caclBMI     
print(a)

I keep getting:

 <class '__main__.calcBMI'>

I know that means I need to convert the object to a string and I'm sure I'm missing something simple.

Thanks for helping if you do so choose to.

7
  • what are you getting when you try to print? Commented Dec 4, 2016 at 3:01
  • The call to __getBMI() in __str__() probably doesn't work since it's not defined. Commented Dec 4, 2016 at 3:06
  • It looks like you're not actually instantiating the object. You need to instantiate an instance of CalcBMI Commented Dec 4, 2016 at 3:07
  • 1
    Also note that __str__ method as defined above returns a 4-tuple (if anything) instead of a string. Commented Dec 4, 2016 at 3:08
  • __str__ has problem in syntax, OP used both comma and plus sign which doesn't make sense! Commented Dec 4, 2016 at 3:21

4 Answers 4

1
a = caclBMI

You've rebound a to the class. You need to instantiate the class instead.

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like you're not actually instantiating the object. You need to instantiate an instance of CalcBMI

...

my_calc_bmi = CalcBMI(my_name, my_age, ...)
print "String representation is: ", my_calc_bmi
print "Or converting explicitly: %s" % (str(my_calc_bmi))

Comments

0

Just implement __str__ and __repr__ methods.

https://docs.python.org/3/reference/datamodel.html#object.str

https://docs.python.org/3/reference/datamodel.html#object.repr

class calcBMI:
    def __str__(self):
        return "The BMI for {} is {}".format(self.__name, self.getBMI())

    def __repr__(self):
        return 'calcBMI({}, {}, {), {})'.format(self.__name, self.__age, self.__height, self.__weight)

...

a = calcBMI("Zogzog", 123, 210, 176)
print(a)

1 Comment

It looks like OP has implemented __str__()
0

You have problem in the following method:

def __str__(self):
        return "The BMI for " + self.__name + " is " + str(self.getBMI())

Since self.getBMI() is returning float value, you need to convert it to string.

Moreover, you need to instantiate the class instead as follows:

a = caclBMI(name, age, weight, height)

For example:

a = calcBMI('Alex', 25, 100, 60)
print(a)

It outputs:

The BMI for Alex is 0.027777777777777776

6 Comments

now I'm getting TypeError: 'str' object is not callable,
I have a = calcBMI(name, age, weight, height)
make sure you are using - self.__name and str(self.getBMI()). if not, please update your code and we will be able to check.
return "The BMI for", self.__name, "is", str(self.__getBMI())
I get the error, AttributeError: 'calcBMI' object has no attribute 'calcBMI_getBMI'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.