Home > database >  calculate bmi and output other inputs in python
calculate bmi and output other inputs in python

Time:02-11

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:

  • Prompt the user repeatedly to enter a patient identification number (an int), patient name (a string), patient height in inches (a positive float), and patient weight in pounds (a positive float) until the user enters a negative number for the patient ID (see Step 5 below for more information on the requirement to repeat the inputs).

  • Be sure to check for validity of the input values for the height and the weight using while loop.

  • For each patient’s information entered, your code should compute the BMI. You must write a function to do this calculation. The function must accept the patient’s height (in inches) and weight (in pounds) as parameters and return the calculated BMI.

  • For each patient’s information entered, output a well-formatted result listing the patient’s ID, name, calculated BMI (output with 1 digit of precision) and associated category (Underweight, Obese, etc.). The use of f-strings to produce the formatted output is recommended.

  • Continue the process of gathering patient data until the user enters a negative number for the patient ID. Remember, this is a type of sentinel control that will require the use of a while loop to control the input process. Most of your application’s logic will effectively be contained within a large while loop (other than the required BMI function and a few other statements). You will have other loops and decision logic nested inside this sentinel control while loop.

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.

CodePudding user response:

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

CodePudding user response:

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()
  • Related