TypeError:不支持*:“function”和“int”BMI calcum的操作数类型

2024-07-04 08:09:47 发布

您现在位置:Python中文网/ 问答频道 /正文

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()

Tags: inyouinputdeffloatdohowbmi
3条回答

编辑

您应该调用函数,请查看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)
GetWeight * 703

因为没有在函数名后面加括号,所以它使用函数对象本身的值,而不是调用函数的结果。你知道吗

函数名后加括号:

GetWeight() * 703

另外,您的GetWeightGetHeight函数没有返回任何值;您需要解决这个问题。你知道吗

为了调用函数,请使用(),如下所示:

GetWeight() , GetHeight() ...

现在,您正在尝试将函数与整数相乘。你知道吗

Read more about functions in Python.

相关问题 更多 >

    热门问题