【问题标题】:I'm trying to make a bmi calculator in python and I don't know what I'm doing wrong我正在尝试用python制作一个bmi计算器,但我不知道我做错了什么
【发布时间】:2020-10-29 08:27:10
【问题描述】:

这是我的代码

weight = float(input("Please Enter Your Weight In KIlograms: "))
height = float(input("Please Enter Your Height In Meters: "))

result = weight / height ** height
print("Your BMI is " + result)

这就是错误

OverflowError: (34, '结果太大')

我还真的不熟悉技术术语,所以你的解释可能需要更具体一点。

【问题讨论】:

  • 如果你像result = (weight / height) ** height这样添加括号
  • 这能回答你的问题吗? stackoverflow.com/questions/20201706/…
  • 你的意思是height**2(高度的平方),而不是height**height(高度的高度次方)
  • @NathanWride 我认为他们的意思更可能是weight / (height * height)(或者只是weight / height**2
  • @MuhammadJunaidHaris 虽然是同样的错误,但原因不同,重复的问题没有回答这个问题......

标签: python calculator


【解决方案1】:

试试这样的

height = float(input("Input your height in meters: "))
weight = float(input("Input your weight in kilogram: "))
print("Your body mass index is: ", round(weight / (height * height), 2))

【讨论】:

    【解决方案2】:

    您的代码需要一些更改。您的最终打印语句尝试连接一个字符串和一个浮点数,即字符串是“您的 BMI 是”,浮点数是结果。因此,您必须使它们都与下面的类型相同。 ("Your BMI is " + str(result))。而且 BMI 等式是体重/身高^2,而不是体重/身高^身高。所以把计算部分改成result = weight / height ** 2

    weight = float(input("Please Enter Your Weight In Kilograms: ")) 
    height = float(input("Please Enter Your Height In Meters: "))
    
    result = weight / height ** 2 
    print("Your BMI is " + str(result))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-22
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 2021-03-27
      • 2014-09-25
      • 1970-01-01
      相关资源
      最近更新 更多