我想在Pythion3中制作一个BMI BMR计算器

时间:2016-03-11 14:35:50

标签: python

好吧,我正在尝试制作一个bmi& bmr计算器。 我已经把代码放下了但是当我选择bmi时,它会通过bmi进程然后在它完成后立即运行mbr,然后程序崩溃了? HALP?

#menu

#Ask weather to print BMI or BMR
output = str(input('Calulate BMI or BMR or Exit:   '))
print (output)


#BMI
if output == 'BMI' or 'bmi':

    #Get height and weight values
    height = int(input('Please enter your height in inches: '))
    weight = int(input('Please enter your weight in pounds: '))
    #Do the first steps of the formula
    heightSquared = (height * height)
    finalWeight = (weight * 703)

    #Fiqure out and print the BMI
    bmi = finalWeight / heightSquared

    if bmi < 18:
        text = 'Underweight'

    if bmi <= 24: # we already know that bmi is >=18 
        text = 'Ideal'

    if bmi <= 29:
        text = 'Overweight'

    if bmi <= 39:
        text = 'Obese'

    else:
        text = 'Extremely Obese'
    print ('Your BMI is: ' + str(bmi))
    print ('This is: ' + text)

#bmr

if output == 'bmr' or 'BMR':

    gender = input('Are you male (M) or female (F) ')
    if gender == 'M' or 'm':

        #Get user's height, weight and age values.
        height = int(input('Please enter your height in inches'))
        weight = int(input('Please enter your weight in pounds'))
        age = int(input('Please enter your age in years'))

        #Figure out and print the BmR
        bmr = 66 + (6.2 * weight) + (12.7 * height) - (6.76 * age)
        print (bmr)

    if gender == 'F' or 'f':
        #Get user's height, weight and age values.
        height = int(input('Please enter your height in inches'))
        weight = int(input('Please enter your weight in pounds'))
        age = int(input('Please enter your age in years'))
        #Figure out and print the BmR
        bmr = 655 + (4.35 * weight) + (4.7 * height) - (4.7 * age)
        print (bmr)


#exit
elif output == 'exit' or 'Exit' or 'EXIT':
   exit()

欢迎任何帮助! 干杯!

1 个答案:

答案 0 :(得分:0)

您的代码中存在许多错误或不一致,以下是一些主要错误。

  • or逻辑运算符不能像这样使用,这在主if语句中最为突出。 Here是一个帮助您开始学习的教程。

  • ifelif使用得非常糟糕,if适用于条件,elif如果后续if hasn'则会运行已经满意了,如果elif中的代码有,而第三个中的代码,else更多是默认语句本身,如果没有满足则转到这个。 Here是关于它的一些文档。

  • 您正在重复使用相同的代码,这应该是固定的。

  • 还有一些其他的花絮将在下面的代码中显示出来,我已经对代码进行了大量评论,以便您能够彻底理解它。

# Menu
# Ask whether to print BMI, BMR, or to exit
output = str(input('Calulate BMI or BMR or Exit: '))
print ('You entered: ' + output) # Try not to print 'random' info

# Exit, I put it up here to make sure the next step doesn't trigger
if output == 'exit' or output == 'Exit' or output == 'EXIT':
   print('Exiting...') # Try to always notify the user about what is going on
   exit()

# Check if the input is valid
if output != 'BMI' and output != 'bmi' and output != 'BMR' and output != 'bmr':
   print('Please enter a valid choice.')
   exit()

# Get user's height, weight and age values
# Never write code more than once, either place it in a function or
# take it elsewhere where it will be used once only
height = int(input('Please enter your height in inches: '))
weight = int(input('Please enter your weight in pounds: '))

# BMI
if output == 'BMI' or output == 'bmi':

   # Do the first steps of the formula
   heightSquared = (height * height)
   finalWeight = (weight * 703)

   # Figure out and print the BMI
   bmi = finalWeight / heightSquared

   if bmi < 18: # First step, is it less than 18?
      text = 'Underweight'

   elif bmi <= 24: # If it isn't is it less than or equal to 24?
      text = 'Ideal'

   elif bmi <= 29: # If not is it less than or equal to 29?
      text = 'Overweight'

   elif bmi <= 39: # If not is it less than or equal to 39?
      text = 'Obese'

   else: # If none of the above work, i.e. it is greater than 39, do this.
      text = 'Extremely Obese'

   print ('Your BMI is: ' + str(bmi))
   print ('This is: ' + text)

# BMR
elif output == 'bmr' or output == 'BMR':

   gender = str(input('Are you male (M) or female (F): '))
   age = int(input('Please enter your age in years: '))
   bmr = 0 # Initialize the bmr 

   if gender == 'M' or gender == 'm':
      # Figure out and print the BMR
      bmr = 66 + (6.2 * weight) + (12.7 * height) - (6.76 * age)

   if gender == 'F' or gender == 'f':
      # Figure out and print the BMR
      bmr = 655 + (4.35 * weight) + (4.7 * height) - (4.7 * age)

   print ('Your BMR is: ' + bmr)