Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

The program ascertains an individual's BMI from the weight and tallness provided utilizing the client's info. Notwithstanding, after I enter 'metric' or 'magnificent' and press enter, the program closes. The initial three print functions turn out great and anything after doesn't seem ensuing to me squeezing the enter button. How would I fix this? 

print('\t\t\t BMI Calculator')

print('\t\t\t By Abdinasir Hussein')

print('\n Hello, this is a BMI Calculator!')

input('Do you wish to enter metric units or imperial units: ')

while input == 'metric':

    height = float(input('Please enter your height input meters(decimals): '))

    weight = int(input('Please enter your weight input kg: '))

    bmi = weight/(height*height)

    if bmi <= 18.5:

        print('Your BMI is', bmi,'which means you are underweight.')

    elif bmi > 18.5 and bmi < 25:

        print('Your BMI is', bmi,'which means you are normal.')

    elif bmi > 25 and bmi < 30:

        print('your BMI is', bmi,'overweight.')

    elif bmi > 30:

        print('Your BMI is', bmi,'which means you are obese.')

    else:

        print('There is an error with your input')

        print('Please check you have entered whole numbers\n'

              'and decimals were asked.')

while input == 'imperial':

    height = int(input('Please enter your height input inputches(whole number): '))

    weight = int(input('Please enter your weight input pounds(whole number): '))

    bmi = (weight*703)/(height*height)

    if bmi <= 18.5:

        print('Your BMI is', bmi,'which means you are underweight.')

    elif bmi > 18.5 and bmi < 25:

        print('Your BMI is', bmi,'which means you are normal.')

    elif bmi > 25 and bmi < 30:

        print('Your BMI is', bmi,'which means you are overweight')

    elif bmi > 30:

        print('Your BMI is', bmi,'which means you are obese.')

    else:

        print('There is an error with your input')

        print('Please check you have entered whole numbers\n'

              'and decimals were asked.')

input('\n\nPlease press enter to exit.')

I've presently transformed it, however, how would I approach altering this block:

input = input('Do you wish to enter metric units or imperial units: ')

if input == 'metric':

    height = float(input('Please enter your height input meters(decimals): '))

    weight = int(input('Please enter your weight input kg: '))

    bmi = weight/(height*height)

    if bmi <= 18.5:

        print('Your BMI is', bmi,'which means you are underweight.')

    elif bmi > 18.5 and bmi < 25:

        print('Your BMI is', bmi,'which means you are normal.')

    elif bmi > 25 and bmi < 30:

        print('your BMI is', bmi,'overweight.')

    elif bmi > 30:

        print('Your BMI is', bmi,'which means you are obese.')

    else:

        print('There is an error with you inputput')

        print('Please check you have entered whole numbers\n'

              'and decimals where asked.')

closed

4 Answers

0 votes
by (25.7k points)
selected by
 
Best answer

Here's an example of a BMI (Body Mass Index) calculator in Python:

def calculate_bmi(weight, height):

    """

    Calculates the BMI (Body Mass Index) given weight in kilograms (kg)

    and height in meters (m).

    """

    bmi = weight / (height ** 2)

    return bmi

def interpret_bmi(bmi):

    """

    Interprets the BMI value and provides a corresponding category.

    """

    if bmi < 18.5:

        category = "Underweight"

    elif 18.5 <= bmi < 25:

        category = "Normal weight"

    elif 25 <= bmi < 30:

        category = "Overweight"

    else:

        category = "Obese"

    return category

# Example usage:

weight = float(input("Enter weight in kg: "))

height = float(input("Enter height in meters: "))

bmi = calculate_bmi(weight, height)

category = interpret_bmi(bmi)

print("BMI:", round(bmi, 2))

print("Category:", category)

In this code, there are two main functions:

  • The calculate_bmi() function takes the weight in kilograms and height in meters as input, and calculates the BMI using the formula: BMI = weight / (height ** 2).
  • The interpret_bmi() function takes the calculated BMI value and interprets it based on standard BMI categories. It returns a corresponding category based on the BMI value.

You can input the weight and height values, and the code will calculate the BMI and provide the corresponding category. The BMI value is rounded to two decimal places for readability.

Please note that this code assumes metric units (kilograms and meters) for weight and height input. If you prefer to use different units, you may need to adjust the calculations accordingly.

0 votes
by (26.4k points)

Instead of using while input== 'imperial':

You can use the below code:

else: 

    height = float(input('Please enter your height input inputches: '))

    weight = int(input('Please enter your weight input pounds: ')) 

You can also use:

elif input == 'imperial':

    height = float(input('Please enter your height input inputches: '))

    weight = int(input('Please enter your weight input pounds: '))

else: 

print("Please enter any one of the units")`

Are you interested to learn the concepts of Python? Join the python training course fast!

0 votes
by (15.4k points)

Here's an implementation of a BMI (Body Mass Index) calculator in Python with a worst-case time complexity of O(1):

def calculate_bmi(weight, height):

    """

    Calculates the BMI (Body Mass Index) given weight in kilograms (kg)

    and height in meters (m).

    """

    bmi = weight / (height ** 2)

    return bmi

def interpret_bmi(bmi):

    """

    Interprets the BMI value and provides a corresponding category.

    """

    if bmi < 18.5:

        category = "Underweight"

    elif 18.5 <= bmi < 25:

        category = "Normal weight"

    elif 25 <= bmi < 30:

        category = "Overweight"

    else:

        category = "Obese"

    return category

# Example usage:

weight = float(input("Enter weight in kg: "))

height = float(input("Enter height in meters: "))

bmi = calculate_bmi(weight, height)

category = interpret_bmi(bmi)

print("BMI:", round(bmi, 2))

print("Category:", category)

The time complexity of this code is O(1) because the calculations performed by the calculate_bmi() function and the interpretation of BMI categories in the interpret_bmi() function are not dependent on the size of the input. Regardless of the weight and height values provided, the code will execute in constant time.

0 votes
by (19k points)

def calculate_bmi(weight, height):

    """

    Calculates the BMI (Body Mass Index) given weight in kilograms (kg)

    and height in meters (m).

    """

    bmi = weight / (height ** 2)

    return bmi

def interpret_bmi(bmi):

    """

    Interprets the BMI value and provides a corresponding category.

    """

    if bmi < 18.5:

        category = "Underweight"

    elif 18.5 <= bmi < 25:

        category = "Normal weight"

    elif 25 <= bmi < 30:

        category = "Overweight"

    else:

        category = "Obese"

    return category

# Example usage:

weight = float(input("Enter weight in kg: "))

height = float(input("Enter height in meters: "))

bmi = calculate_bmi(weight, height)

category = interpret_bmi(bmi)

print("BMI:", round(bmi, 2))

print("Category:", category)

In this code, both the calculate_bmi() and interpret_bmi() functions have a constant time complexity of O(1). The calculations performed and the interpretation of BMI categories do not depend on the size of the input.

Therefore, regardless of the weight and height values provided, the code will execute in constant time, resulting in the best possible time complexity.

Related questions

Browse Categories

...