怎样用python编写bmi的代码:需要帮助编写pythonBMIcalc

我是 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)
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 声明为全局变量

2

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

def BMI():
    global bmi
    ...

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

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(180)
C63空开能带多少千瓦:php中的二进制 为什么是63位(binary 63)
上一篇
怎么看cpu频率:xkcd api 怎么看解释
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(24条)