doubleD
doubleD

Reputation: 269

calculate bmi and output other inputs in python

I am working on a python code to calculate bmi and to perform other user inputs as well. I have the code below using the OOP approach, but I think I am still missing some parts and is not working as expected. The expectations are:

With some googling and sort of modifying it.

class BodyMassIndex:

    def __init__(self, pid, name, weight, height):
        self.patient_id = pid
        self.name = name
        self.weight = weight
        self.height = height

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

    @property
    def score(self):
        if 18.5 < self.body_mass_index < 25:
            return 'normal weight'
        elif 25 < self.body_mass_index < 30:
            return 'overweight'
        elif self.body_mass_index > 30:
            return 'obese'
        else:
            return 'underweight'

    def print_score(self):
        ##print('For {}:{}'.format(self.patient_id, self.name))
        print('Your Body Mass Index score is: {}'.format(self.body_mass_index))
        print('You are {}'.format(self.score))


def _get_user_info():
    while True:
        try:
            pid = int(input("Please enter your 5-digit patient id: "))
            p_name = str(input("Please enter patient first name and last name: "))            
            weight = float(input('Enter weight in pounds: '))
            height = float(input('Enter height in inches: '))

            if 0 < weight and 0 < height < 107:
                return weight, height
            else:
                raise ValueError('Invalid height or weight. Let us start over.')
        except ValueError:
            print('Invalid height or weight input')
            continue


def calculate_bmi():
    pid, name, weight, height = _get_user_info()
    return BodyMassIndex(weight, height)

if __name__ == '__main__':
    bmi = calculate_bmi()
    bmi.print_score()

After entering the four values, I get a valuetype error: expecting 4 but got 2, or something sounds like that. I appreciate your help in making this work.

Upvotes: 0

Views: 1809

Answers (2)

qmorgan
qmorgan

Reputation: 85

The return statement was missing the pid and the p_name values at lines 39 and 49.

The new code looks like this:

class BodyMassIndex:

    def __init__(self, pid, name, weight, height):
        self.patient_id = pid
        self.name = name
        self.weight = weight
        self.height = height

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

    @property
    def score(self):
        if 18.5 < self.body_mass_index < 25:
            return 'normal weight'
        elif 25 < self.body_mass_index < 30:
            return 'overweight'
        elif self.body_mass_index > 30:
            return 'obese'
        else:
            return 'underweight'

    def print_score(self):
        ##print('For {}:{}'.format(self.patient_id, self.name))
        print('Your Body Mass Index score is: {}'.format(self.body_mass_index))
        print('You are {}'.format(self.score))


def _get_user_info():
    while True:
        try:
            pid = int(input("Please enter your 5-digit patient id: "))
            p_name = str(input("Please enter patient first name and last name: "))            
            weight = float(input('Enter weight in pounds: '))
            height = float(input('Enter height in inches: '))

            if 0 < weight and 0 < height < 107:
                return pid, p_name, weight, height
            else:
                raise ValueError('Invalid height or weight. Let us start over.')
        except ValueError:
            print('Invalid height or weight input')
            continue


def calculate_bmi():
    pid, name, weight, height = _get_user_info()
    return BodyMassIndex(pid, name, weight, height)

if __name__ == '__main__':
    bmi = calculate_bmi()
    bmi.print_score()

Upvotes: 1

baines
baines

Reputation: 31

The problem lies in your _get_user_info function. I added the missing entries in the function below. Also, as Tim Roberts said, you don't need to wrap input with str. input function returns a string in python 3. Find the new code below

def _get_user_info():
    while True:
        try:
            pid = int(input("Please enter your 5-digit patient id: "))
            p_name = input("Please enter patient first name and last name: ")          
            weight = float(input('Enter weight in pounds: '))
            height = float(input('Enter height in inches: '))

            if 0 < weight and 0 < height < 107:
                return pid, p_name,weight, height
            else:
                raise ValueError('Invalid height or weight. Let us start over.')
        except ValueError:
            print('Invalid height or weight input')
            continue

Upvotes: 2

Related Questions