简体   繁体   中英

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

The program runs until the calculate module where I get the error:

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

Due to your suggestions the code now looks like this:

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

I have revised the code, however now the program is stuck in a continuous question/answer loop and the calculate module never starts.

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

In order to call your functions use () like so:

GetWeight() , GetHeight() ...

As it is right now, you are trying to multiply a function with an integer.

Read more about functions in Python.

GetWeight * 703

Since you didn't put parentheses after the function name, it's using the value of the function object itself instead of the result of calling the function .

Put parentheses after the function name:

GetWeight() * 703

Also, your GetWeight and GetHeight functions aren't returning any values; you'll need to fix that.

EDIT

You should be calling the functions, check out this corrected code for 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM