简体   繁体   English

Python中的BMI计算器

[英]BMI Calculator in Python

The program calculates a person's BMI from the weight and height supplied using the user's input.该程序根据用户输入提供的体重和身高计算出一个人的 BMI。 However, after I enter 'metric' or 'imperial' and press enter, the program closes.但是,在我输入“公制”或“英制”并按 Enter 后,程序将关闭。 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':而不是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 .你不应该使用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 就像

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 :)并且不要忘记 bmi = 25 和 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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