【问题标题】:TypeError: unsupported operand type(s) for *: 'function' and 'int' BMI CalculatorTypeError: *: 'function' and 'int' BMI Calculator 不支持的操作数类型
【发布时间】:2016-02-17 21:01:51
【问题描述】:
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()

【问题讨论】:

  • 除了功能的问题,你认为你需要eval吗?
  • 我以为eval是用来让python计算方程的。我错了吗?

标签: python error-handling typeerror


【解决方案1】:

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

GetWeight() , GetHeight() ...

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

Read more about functions in Python.

【讨论】:

    【解决方案2】:
    GetWeight * 703
    

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

    在函数名后加括号:

    GetWeight() * 703
    

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

    【讨论】:

    • 在每个函数的末尾,添加一行return X,其中X是您要返回的值。
    • 所以它应该在第一个函数的末尾读为return GetHeight
    【解决方案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)
    

    【讨论】:

    • 这里没有理由使用 eval。
    • 添加您的建议时,程序现在会询问体重,然后是身高,然后再次询问体重。为什么要这样做?
    • @RABucc,每次您拨打GetWeightGetHeight 时,它都会要求用户输入。您应该保存收到的响应并使用它,在此处查看更新的代码。
    • 我从此代码得到的错误:def Calculate(): weight = GetWeight() height = GetHeight() BMI = (weight * 703) / (height * height) print ("Your BMI is", BMI) main()
    • Traceback (most recent call last): File "C:/Users/kirby.DESKTOP-AHMTFQK/AppData/Local/Programs/Python/Python35/BMI Calculator.py", line 27, in <module> main() File "C:/Users/kirby.DESKTOP-AHMTFQK/AppData/Local/Programs/Python/Python35/BMI Calculator.py", line 9, in main Calculate() File "C:/Users/kirby.DESKTOP-AHMTFQK/AppData/Local/Programs/Python/Python35/BMI Calculator.py", line 25, in Calculate BMI = (weight * 703) / (height * height) TypeError: can't multiply sequence by non-int of type 'str'
    猜你喜欢
    • 2012-10-07
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 2014-03-31
    • 2012-12-12
    • 2020-02-27
    • 2021-05-23
    相关资源
    最近更新 更多