Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';t修复我的体重指数计算器上的计算模块_Python - Fatal编程技术网

Python Can';t修复我的体重指数计算器上的计算模块

Python Can';t修复我的体重指数计算器上的计算模块,python,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?

为了消除这个错误,我反复修改了代码。对不起,如果这件事真的很简单,我想自学。谢谢你的关注

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'

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

在您的
GetHeight
方法中,您没有将其设置为浮动,类似于您在
GetWeight
方法中所做的:

更改此项:

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()
为此:

height = input("Enter your height in inches:\n ")
另外,您还可以删除
main
中的调用
GetWeight
GetHeight
。您的
Calculate
方法已在以下几行为您处理此问题:

height = float(input("Enter your height in inches:\n "))
因此,代码将是:

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

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

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
已清理版本:

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

请注意,在Python中,模块是一个文件,您的
Calculate
是一个函数。此外,这甚至不应该运行,因为在
def GetWeight()
之后没有缩进。当我修复缩进时,它运行平稳。它不能正常工作,因为它坚持要求我每个人输入两次体重和身高,但它会运行。您不需要从
intro
中调用
GetWeight
GetHeight
Calculate
已经调用了它们。您确定您的实际代码没有
weight=GetWeight
位置,即没有
()
?这可以解释您报告的错误。虽然这是代码的一个问题,但它不会产生错误消息OP is reporting。@tobias_k在修改后,Python 3.4上的代码运行良好。你那边发生了什么?谢谢你的意见。我已经做了改变,但我仍然无法获得计算BMI的程序。如何修复calculate部分?它肯定会(没有尝试),但OP似乎得到了一个不同的错误,因此问题似乎有问题。@idjaw当我使用更改运行它时,仍然得到了
TypeError:不支持的操作数类型*:“function”和“int”
错误。我使用的是3.5.1将高度转换为浮点数。基于OP的原始错误,我认为缩进是因为复制粘贴问题。我不认为OP在他们的编辑器中看到了这一点。缩进是否会导致GetWeight未正确声明,从而导致weight=GetWeight()将函数分配给weight,而不是整数?这就解释了错误信息。错误的缩进将给出准确的结果:缩进错误。我敢肯定OP在将代码移植到他们的问题时犯了一个间隔错误。哦,可能是这样。代码运行良好(除了多次调用以获取体重和身高),因此问题的措辞可能不恰当。就这样吧。
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()
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()