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

该程序一直运行到calculate模块出现错误为止:
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()

我已经修改了代码,但是现在程序陷入了连续的问题/答案循环,并且calculate模块从不启动。
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()

最佳答案

编辑

您应该调用函数,查看此更正后的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)

关于python - TypeError : unsupported operand type(s) for *: 'function' and 'int' BMI Calculator,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35417207/

10-12 07:32