此功能在我的代码中起什么作用?

时间:2019-01-30 00:00:06

标签: python

    # BMI calculator

name1 = "Fahad"
weight_kg1 = "40.8"
height_m1 = "1.7"

name2 = "Rardo"
weight_kg2 = "140"
height_m2 = "1.9"

name3 = "Billy"
weight_kg3 = "70"
height_m3 = "2.2"

bmi_calculator(name,height,weight):
BMI = height_kg *(height_m * * 2)
print(bmi:)
if bmi < 25
        return name + "is not over weight"
    else:
        return name + "is overweight"

result1 = "name1, height_m1, weight_kg1"
result2 = "name2, height_m2, weight_kg2"
result3 = "name1, height_m1, weight_kg3"

我不知道函数“ BMI计算器”的作用(第15行) 我也不理解“结果1 =“名称1,...........””(上面是哪个)的作用

如果我能解释这两个在我的代码中的作用以及该函数如何连接回我的代码,我将非常有责任。

谢谢。

1 个答案:

答案 0 :(得分:2)

在python中定义函数的语法:

def function_name(parameters):

您的函数名称缺少定义该函数的 def 关键字。在此处阅读更多信息:https://docs.python.org/2/tutorial/controlflow.html#defining-functions

请密切注意缩进的一致性 ,每个语句的间距为4,缩进的数量是可选的,但是请确保每个缩进的宽度相同

下面纠正了代码中的其他错误,请注意从数值中删除引号(" "):

# BMI calculator

name1 = "Fahad"
weight_kg1 = 40.8  # removed "" from all 
height_m1 = 1.7    # these weight/height values

name2 = "Rardo"
weight_kg2 = 140 
height_m2 = 1.9   

name3 = "Billy"
weight_kg3 = 70
height_m3 = 2.2

def bmi_calculator(name,height,weight):
    BMI = weight * (height ** 2)  # exponent is `**` not `* *`
    print(BMI)                    # this prints the numerical values from the line above (see output below)
    if BMI < 25:
        return name + "is not over weight"
    else:
        return name + "is overweight"


result1 = bmi_calculator(name1, height_m1, weight_kg1)
result2 = bmi_calculator(name2, height_m2, weight_kg2)
result3 = bmi_calculator(name1, height_m1, weight_kg3)

print(result1)
print(result2)
print(result3)

输出:

117.912
505.4
338.8
Fahadis overweight
Rardois overweight
Billyis overweight

编辑:BMI公式似乎不正确(公式:weight (kg) / [height (m)]**2 https://www.cdc.gov/healthyweight/assessing/bmi/adult_bmi/index.html#Interpreted),产生不正确的输出值,但我将作为数学练习留给您。

希望这说明了更正。如果需要进一步说明,请随时发表评论。