无法在我的BMI计算器上修复计算模块

时间:2016-02-15 21:06:33

标签: python

def main():
  intro()
  GetWeight()
  GetHeight()
  Calculate()

def intro():
  print ("Welcome to the BMI Calculator by Rabucc")
  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)
main()

我一遍又一遍地修改了代码,试图摆脱这个错误。对不起,如果这件事真的很简单,我正在努力教自己。谢谢你的期待。

line 25, in Calculate
BMI = (weight * 703) / (height * height)
TypeError: unsupported operand type(s) for *: 'function' and 'int'

修改我现在的代码后

def main():
intro()
GetWeight()
GetHeight()
Calculate()

def intro():
print ("Welcome to the BMI Calculator by Rufus Hunt")

def GetWeight ():
weight = float(input("How much do you weigh in pounds?\n "))
return weight

def GetHeight ():
height = float(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)
main()

这导致程序重复这两个问题,然后计算BMI。为什么要重复这些问题?

4 个答案:

答案 0 :(得分:1)

清理版本:

def get_float(prompt):
    while True:
        try:
            return float(input(prompt))
        except ValueError:
            pass

def bmi(weight, height):
    return 703. * weight / height**2

def main():
    print("Welcome to the BMI Calculator by Rabucc")
    weight = get_float("How much do you weigh (in pounds)? ")
    height = get_float("How tall are you (in inches)? ")
    print("Your BMI is {:.1f}".format(bmi(weight, height)))

if __name__=="__main__":
    main()

答案 1 :(得分:0)

在你的GetWeight方法中,你没有投射到浮点数,类似于你在height = input("Enter your height in inches:\n ") 方法中所做的那样:

改变这个:

height = float(input("Enter your height in inches:\n "))

到此:

GetWeight

另外,您可以在GetHeight内删除调用mainCalculate的额外信息。您的height = GetHeight() weight = GetWeight() 方法已经在以下方面为您解决了这个问题:

def main():
  intro()
  Calculate()

def intro():
  print ("Welcome to the BMI Calculator by Rabucc")
  def GetWeight():
  weight = float(input("How much do you weigh in pounds?\n "))
  return weight

def GetHeight():
  height = float(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)
main()

所以,代码将是:

Welcome to the BMI Calculator by Rabucc
Enter your height in inches:
 5
How much do you weigh in pounds?
 5
Your BMI is 140.6

演示:

Macaw.php

答案 2 :(得分:0)

您不需要缩进GetWeight函数定义。缩进对Python很重要。试试这个。

def main():
  intro()
  Calculate()

def intro():
  print ("Welcome to the BMI Calculator by Rabucc")

def GetWeight():
  weight = float(input("How much do you weigh in pounds?\n "))
  return weight

def GetHeight():
  height = float(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)
main()

答案 3 :(得分:0)

def main():
  intro()
  weight = GetWeight()
  height = GetHeight()
  Calculate(weight, height)

def intro():
  print ("Welcome to the BMI Calculator by Rabucc")

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(lb,inches):
  BMI = (lb * 703) / (lb * inches)
  print ("Your BMI is", BMI)

main()