需要帮助编写python BMI计算

时间:2013-11-30 08:05:43

标签: python

我是python的新手,目前正在学习正确使用函数。

我的任务是使用python制作一个bmi计算器作为我的作业。这个bmi计算器必须提供是否使用公制或英制单位的选项。根据此选项,它将以可敬的单位类型询问您的体重和身高。这就是问题统计数据 - 无论我输入什么,它仍然使用公制单位,似乎忽略了我的if / elif / else语句。所以这是第一个问题,我无法理解我在哪里出错了。

在问题解决之后,它计算的bmi需要被放入一个类别,然后程序应该告诉你bmi适合哪个类别,这部分甚至不适用于公尺bmi它只是说:

    """this is the error i keep getting, the actual code for the program is below""" 
    traceback (most recent call last):
     File "C:/Python33/bmi calculator 2 2 2 2.py", line 54, in <module>
        catagory(bmi)
     File "C:/Python33/bmi calculator 2 2 2 2.py", line 45, in catagory
        if bmi <18.5:
    TypeError: unorderable types: tuple() < float()


"""this is a bmi calculator that works in both imperial and metric units
and can tell your bmi category"""
bmi = ()
metric = ("metric")
Metric = ("Metric")
imperial = ("imperial")
Imperial = ("Imperial")
MetricBMI = ()
ImperialBMI = ()

answer = input (" Do you want to work out you BMI using Metric or Imperial units?")



def BMI():

    metric = ("metric")
    Metric = ("Metric")
    imperial = ("imperial")
    Imperial = ("Imperial")


    if answer.lower() == metric:      
        print("You have chose to calculate your bmi in metric units")
        Weight_kg = float(input("What is your weight in kilograms (kg)"))
        Height_m = float(input("What is your height in meters (m)?"))
        bmi = Weight_kg/Height_m/Height_m
        print("Your BMI is " + str(bmi))

    elif answer.lower() == imperial:
        print ("You have chosen to calculate your bmi in imperial units")
        Weight_lbs = float(input("What is your weight in pounds (lbs)?"))
        Height_inches = float(input("What is your height in inches??"))
        bmi = Weight_lbs*703/Height_inches/Height_inches
        print ("Your BMI is " + str(bmi))
    else:
        print ("please restart and enter either 'imperial' or 'metric'")


BMI()


def catagory(bmi):

    if bmi <18.5:
        return ("You are underweight")
    elif bmi >=18.5 and bmi <25:
        return ("You are normal weight")
    elif bmi >= 25 and bmi <30:
        return ("you are overweight")
    elif bmi >=30:
        return ("you are obese")

catagory(bmi)

2 个答案:

答案 0 :(得分:4)

这是你想做的吗?

def catagory(BMI):

    if BMI < 18.5:
        print "You are underweight"
    elif BMI >= 18.5 and BMI <25:
        print "You are normal weight"
    elif BMI >= 25 and BMI <30:
        print "you are overweight"
    elif BMI >= 30:
        print "you are obese"

def BMI():
    choice = raw_input("SI or Imperial? ")
    weight = int(raw_input("weight: "))
    height = int(raw_input("height: "))

    if choice == "SI":
        BMI = weight / (height * height)

    if choice == "Imperial":
        BMI = (weight * 703) / (height * height)
    return BMI

BMI = BMI()
catagory(BMI)

您的BMI功能不会返回为BMI计算的值。要更改此设置,您可以让函数将BMI的值返回到主脚本,如上面的“返回BMI”所示。或者您可以在BMI函数中声明BMI是一个全局变量

答案 1 :(得分:2)

由于您的BMI函数正在修改全局变量bmi,因此需要声明它:

def BMI():
    global bmi
    ...

如果没有global声明,Python会创建一个本地bmi变量,该函数在函数完成时会被遗忘。您看到的错误是尝试使用全局bmi变量的未更改初始值的结果。您最好完全删除此初始化或完全删除全局变量。