简体   繁体   中英

BMI Calculator in Python

The program calculates a person's BMI from the weight and height supplied using the user's input. However, after I enter 'metric' or 'imperial' and press enter, the program closes. The first three print functions work fine and anything after doesn't appear subsequent to me pressing the enter button. How do 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 now changed it, but how do I go about editing 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.')

Instead of while input == 'imperial':

Use

else: 
    height = float(input('Please enter your height input inputches: '))
    weight = int(input('Please enter your weight input pounds: ')) 

Or 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")`

You shouldn't use while . You meant to write:

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

if units == 'metric':
    ......
elif units == 'imperial':
    ......

input is a function not variable. You have to assign input to a variable before comparing.

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

then

if (in == "Metric")

you can use if again just like

if units == 'metric':
    height = ...
    weight = ...
    bmi = ...

    if bmi <= 18.5:
        print... 
    elif bmi > 18.5 and <= 25:
        print...
    .....
elif units == 'imperial':
    height = ...
    weight = ...
    bmi = ...

    if bmi <= 18.5:
        print...
    elif bmi..

    ....
else:
    print('plese enter one units.')

and don't forget bmi = 25 and 30 :)

height = float(input("Your height in metres:"))
weight = int(input("Your weight in kilograms:"))
bmi = round(weight/ (height * height), 1)

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')
        # this calculates Body Mass Index using Metrics standard
    def bmi_metrics():
        weight = float(input("enter your mass in kilogram (KG) : " ))
        height = float(input("enter your height in metres(M) : " ))
        bmi = weight * (height**2)
        print("your body mass index = ".capitalize(), bmi, " kg/m2")
    
        if bmi <= 18.5 :
            print("you are below standard body mass, you should eat food rich in fats and eat regularly".capitalize())
            bmi_calculator()
    
        elif bmi >= 18.5 and bmi <= 24.9:
            print("you have a normal standard weight".upper())
            bmi_calculator()
    
        elif bmi >= 25 and bmi <= 29.9:
            print("you over weighed normal standard weight".upper())
            bmi_calculator()
    
        else: 
            print("you have obesity and needs quick medical attention".upper())
            response = input("Do you wish to continue? type Y for (yes) or N for (no): ")
            if response == "y" or response == "Y":
                bmi_calculator()
            elif response == "n" or response == "N":
                print(" Thanks for using this program ")
                exit()

    # This calculates Body Mass Index using Imperial standard
def bmi_imperial():
    weightInKilogram = float(input("enter your mass in kilogram (kg) : "))
    heightInMeters = float(input("enter your height in metres(m) : " ))
    weight = weightInKilogram * 2.2  # covert kg to pounds
    height = heightInMeters / 0.0254 # convert meters to inches
    bmi = (weight * 703)/(height**2)
    print("your weight in pounds = ", weight, "lbs")
    print("your height in inches = ", height, "inches")
    print("your body mass index = ".capitalize() , bmi, "lbs/inches2")

    if bmi <= 18.5 :
        print("you are below standard body mass, you should eat food rich in fats and eat regularly".capitalize())

    elif bmi >= 18.5 and bmi <= 24.9:
        print("you have a normal standard weight".capitalize())
        bmi_calculator()

    elif bmi >= 25 and bmi <= 29.9:
        print("you over weighed normal standard weight".capitalize())
        bmi_calculator()

    else: 
        print("you have obesity and needs quick medical attention".capitalize())
        response = input("Do you wish to continue? type Y for (yes) or N for (no): ")
        if response == "y" or response == "Y":
            bmi_calculator()
        elif response == "n" or response == "N":
            print(" Thanks for using this program ")
            exit()

    # This is the main and it computes the BMI with either standard based on user response
def bmi_calculator():
    print("""
    ******************************************************************
            Welcome to Body Mass Index Calculator (BMI = M/H**2)
    ******************************************************************
    """)

    #Ask if user would like to use Metric or Imperial units
    response = input("would you like to calculate your Body Mass Index in Metrics or Imperial, type Metrics or Imperial: ").lower()
    
    if response == 'metrics':
        print("---------you have chosen to compute your BMI in---------", response.upper())
        bmi_metrics() # This calls and computes the metrics standard function

    elif response == 'imperial':
        print("---------you have chosen to compute your BMI in---------", response.upper())
        return bmi_imperial()# This calls and computes the imperial standard function

    else:
        print("invalid response please try again".capitalize())
        bmi_calculator()
bmi_calculator()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM