【问题标题】:range class is not working properly范围类无法正常工作
【发布时间】:2018-04-22 14:43:41
【问题描述】:

下面我提到了我的数据,我试图使用范围类 (19-25) 作为输出,但不知何故我不理解它。 - 项目清单 如果输出小于 19,我希望得到这样的结果 如果输出在 19-25 之间,则说“你瘦得像铁轨” 如果输出大于 25 然后说“你像鹧鸪”

这是我的输出数据

a=float(input('what is your weight(kg)'))
b=float(input('what is your height(cm)'))
operations=[(a)/(b*b/100)*100]

output=operations
print("Your BMI is", output, "you are skinny as a rail.")

【问题讨论】:

  • edit your questionformat it properly。格式对于缩进很重要的 Python 代码非常重要。
  • 为什么你的代码提倡身体羞辱? ://
  • 还要注意range不是函数,它是a class
  • 很抱歉格式不好。我是 stackover flow 的新手,这就是为什么我不明白如何很好地格式化它。
  • 您在询问如何使用范围,但您从帖子中编辑了包含它的唯一行?

标签: python-3.x


【解决方案1】:

我不完全理解您的 BMI 公式,但 Metric 中的正确公式是

BMI = Weight / (Height)^2

我在我的示例中使用了它,但您可以根据自己的喜好随意更改它。也不需要将操作转换为列表。如果有人决定输入字母而不是数字,您的代码将不起作用,因此最好验证一下。

>>> def bmi ():
...     try:
...         weight = float (input ('Weight in Kgs: '))
...         height = float (input ('Height in Meters: '))
...         bmi = weight / (height * height)
...         if bmi < 19:
...             print ('Your BMI: %.2f.  You\'re skinny.' % bmi)
...         elif 19 <= bmi <= 25:
...             print ('Your BMI: %.2f.  You\'re fit.' % bmi)
...         else:
...             print ('Your BMI: %.2f.  You\'re plum.' % bmi)
...     except ValueError:
...         print ('Only numbers are accepted!')
... 
>>>

输出

>>> bmi ()
Weight in Kgs: 80
Height in Meters: 1.7
Your BMI: 27.68.  You're plum.

>>> bmi ()
Weight in Kgs: 60
Height in Meters: 1.7
Your BMI: 20.76.  You're fit.

>>> bmi ()
Weight in Kgs: 50
Height in Meters: 1.8
Your BMI: 15.43.  You're skinny.

>>> bmi ()
Weight in Kgs: 65
Height in Meters: a
Only numbers are accepted!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-29
    • 2011-03-08
    • 2013-03-14
    相关资源
    最近更新 更多