TypeError: unsupported operand type(s) for *: 'function' and 'int' BMI Calculator
TypeError: unsupported operand type(s) for *: 'function' and 'int' BMI Calculator
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()
我已经修改了代码,但是现在程序卡在一个连续的 question/answer 循环中,计算模块永远不会启动。
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()
为了调用您的函数,请像这样使用 ()
:
GetWeight() , GetHeight() ...
现在,您正在尝试将 函数 与整数相乘。
编辑
您应该调用这些函数,查看 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
此外,您的 GetWeight
和 GetHeight
函数没有返回任何值;你需要解决这个问题。
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()
我已经修改了代码,但是现在程序卡在一个连续的 question/answer 循环中,计算模块永远不会启动。
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()
为了调用您的函数,请像这样使用 ()
:
GetWeight() , GetHeight() ...
现在,您正在尝试将 函数 与整数相乘。
编辑
您应该调用这些函数,查看 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
此外,您的 GetWeight
和 GetHeight
函数没有返回任何值;你需要解决这个问题。