簡體   English   中英

Python中的BMI計算器

[英]BMI Calculator in Python

該程序根據用戶輸入提供的體重和身高計算出一個人的 BMI。 但是,在我輸入“公制”或“英制”並按 Enter 后,程序將關閉。 前三個打印功能工作正常,在我按下回車按鈕后沒有出現任何后面的內容。 我該如何解決這個問題?

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.')

我現在已經改變了它,但是我該如何編輯這個塊:

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.')

而不是while input == 'imperial':

使用

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

或使用

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

你不應該使用while 你的意思是寫:

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

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

輸入是一個不可變的函數。 在比較之前,您必須將輸入分配給變量。

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

那么

if (in == "Metric")

你可以再次使用 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.')

並且不要忘記 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