在函数中未定义回溯和NameErrors。

时间:2015-11-17 18:25:23

标签: python

我对Python编程很陌生,今年刚刚开始。我目前正在围绕python中的函数做一个项目,而且我遇到了问题。继承了我到目前为止所做的事情,但它却陷入了第三个定义。

def userWeight():
    weight = input("Enter your weight in pounds:")

def userHeight():
    height = input("Enter your height in inches:")

def convertWeightFloat(weight):
    return float(weight)
    weight = float(weight)

def convertHeightFloat(hf):
    return float(height)

def calculateBMI():
    BMI = (5734 * weight_float) / (height_float**2.5)
    return BMI

def displayBMI():
    print("Your BMI is:",BMI)

def convertKG():
   return weight_float * .453592

def convertM():
    return height_float * .0254

def calculateBMImetric():
    return 1.3 * weight_kg / height_meters**2.5

def displayMetricBMI():
    print("Your BMI using Metric calculations is: ", BMImetric)

def main():
    userWeight()
    userHeight()
    convertWeightFloat(weight)
    convertHeightFloat()
    calculateBMI()
    displayBMI()
    convertKG()
    convertM()
    calculateBMImetric()
    displayMetricBMI()

main()

这是我每次尝试运行时收到的错误消息......

Enter your weight in pounds:155
Enter your height in inches:70
Traceback (most recent call last):
  File "C:/Users/Julian/Desktop/Python Stuff/ghp17.py", line 45, in <module>
    main()
  File "C:/Users/Julian/Desktop/Python Stuff/ghp17.py", line 36, in main
    convertWeightFloat(weight)
NameError: name 'weight' is not defined

现在我可能尝试了几种不同的东西,每种东西都给我不同的错误。

任何帮助人员?

3 个答案:

答案 0 :(得分:3)

main函数中,您传递的是weight,但尚未对其进行定义。您可能希望保存userWeight

中的值
def userWeight():
    weight = input("Enter your weight in pounds:")
    return weight

def main():
    weight = userWeight()
    ...
    convertWeightFloat(weight)

在计算之前,您还会返回weight

def convertWeightFloat(weight):
    return float(weight)
    weight = float(weight)

计算后移动return语句:

def convertWeightFloat(weight):
    weight = float(weight)
    return float(weight)

答案 1 :(得分:2)

这里有一些问题。

首先,在convertWeightFloat(weight)尚未定义的情况下,您致电weight。这是因为weight仅存在于您的函数userWeight中(它被称为函数的范围)。如果您希望在程序的主要部分中知道weight,则需要在那里定义它。

weight = userWeight()
...

仅当函数userWeight返回值时才有效:

def userWeight():
    weight = input("Enter your weight in pounds:")
    return weight

height相同的问题。

此外,在函数convertWeightFloat中,return语句是将要执行的最后一件事。在该行之后,程序退出该函数,从而永远不会执行最后一行:

def convertWeightFloat(weight):
    weight = float(weight)
    return weight

基本上,您在函数内使用的每个变量都应该提供给函数(大多数时候作为参数)。并且您的所有函数都应返回已处理的值。这是您的程序的工作版本:

def userWeight():
    weight = input("Enter your weight in pounds:")
    return weight

def userHeight():
    height = input("Enter your height in inches:")
    return height

def convertWeightFloat(weight):
    return float(weight)

def convertHeightFloat(height):
    return float(height)

def calculateBMI(weight_float, height_float):
    BMI = (5734 * weight_float) / (height_float**2.5)
    return BMI

def displayBMI(BMI):
    print("Your BMI is:",BMI)

def convertKG(weight_float):
   return weight_float * .453592

def convertM(height_float):
    return height_float * .0254

def calculateBMImetric(weight_kg, height_meters):
    return 1.3 * weight_kg / height_meters**2.5

def displayMetricBMI(BMImetric):
    print("Your BMI using Metric calculations is: ", BMImetric)

def main():
    weight = userWeight()
    height = userHeight()
    weight_float = convertWeightFloat(weight)
    height_float = convertHeightFloat(height)
    bmi = calculateBMI(weight_float, height_float)
    displayBMI(bmi)

答案 2 :(得分:0)

在你打电话的主()中

convertWeightFloat(重量)

但没有定义重量