Python中的BMI计算器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20405610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:21:21  来源:igfitidea点击:

BMI Calculator in Python

python

提问by oneabdi

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?

该程序根据用户输入提供的体重和身高计算出一个人的 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.')

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

回答by Simeon Visser

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

你不应该使用while. 你的意思是写:

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

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

回答by rafee

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

回答by harryp

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 :)

回答by Kawin M

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

回答by Amogh Nath

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