TypeError:*:'function'和'int'BMI Calculator的不支持的操作数类型

时间:2016-02-15 19:08:27

标签: python error-handling typeerror

def GetWeight():
GetWeight = 0.0   
Weight = float(input("How much do you weigh in pounds?\n "))
def GetHeight():
Heightinches = 0.0
Heightinches = input("Enter your height in inches: ")

def Calculate():
BMI = eval (GetWeight * 703 / (GetHeight * GetHeight))
print ("Your BMI is", BMI)
main()

程序一直运行到我得到错误的计算模块:

TypeError: unsupported operand type(s) for *: 'function' and 'int'

由于您的建议,代码现在看起来像这样:

def GetWeight():
GetWeight = 0.0   
Weight = float(input("How much do you weigh in pounds?\n "))

def GetHeight():
Heightinches = 0.0
Heightinches = input("Enter your height in inches:\n ")

def Calculate():
BMI = eval (GetWeight() * 703 / (GetHeight() * GetHeight()))
print("Your BMI is", BMI)
main()

我修改了代码,但是现在程序停留在连续的问题/答案循环中,计算模块永远不会启动。

def GetWeight():
GetWeight = 0.0   
Weight = float(input("How much do you weigh in pounds?\n "))
return GetWeight
def GetHeight():
Heightinches = 0.0
Heightinches = input("Enter your height in inches:\n ")
return GetHeight
def Calculate():
BMI = eval (GetWeight() * 703 / (GetHeight() * GetHeight()))
print("Your BMI is", BMI)
main()

3 个答案:

答案 0 :(得分:0)

要调用您的函数,请使用(),如下所示:

GetWeight() , GetHeight() ...

就像现在一样,你试图将函数与整数相乘。

Read more about functions in Python.

答案 1 :(得分:0)

GetWeight * 703

由于你没有在函数名后添加括号,所以它使用函数对象本身的值而不是调用函数的结果

将括号放在函数名后面:

GetWeight() * 703

此外,您的GetWeightGetHeight函数未返回任何值;你需要解决这个问题。

答案 2 :(得分:-1)

修改

您应该调用这些函数,查看Calculate

的更正代码
def GetWeight():
  weight = float(input("How much do you weigh in pounds?\n "))
  return weight

def GetHeight():
  height = input("Enter your height in inches:\n ")
  return height

def Calculate():
  height = GetHeight()
  weight = GetWeight()
  BMI = (weight * 703) / (height * height)
  print ("Your BMI is", BMI)