下面是我的代码:
def inForm():
name = requestString("What is your name?")
age = requestInteger("What is your age?")
height = requestInteger("What is your height?")
weight = requestInteger("What is your weight?")
print "Hello", name, "!", "You are", age,"years old!", "Your height
is", height, "cm", "and you weigh", weight, "kg"
我需要将BMI计算添加到此代码中,这是我的公式,BMI=体重/(身高*身高)
根据BMI显示这些信息:
你的体重严重不足15
你的体重严重不足,从15.0到16.0
你的体重从16.0减到18.5
你的体重在18.5到25 之间是正常的
你超重25到30
你是中度肥胖30至35
你从35岁到40岁严重肥胖
你是非常严重的肥胖超过40
我似乎想不通,我该怎么办?
提前感谢!
为什么不使用requestNumber来表示身高和体重呢。此外,打印声明可以像我下面演示的那样更短。另外,我使用%s%d%r这被称为字符串格式化操作。
def inForm():
name = requestString("Please enter your name")
age = requestInteger("How old are you?")
height = requestNumber("How tall are you? (Meters)")
weight = requestNumber("How heavy are you? (Kilograms)")
bmi = weight/(height*height)
print "Hello, %s! I see that you are %r years old, %r meters tall, weight %r kgs and your BMI is %d." % (name, age, height, weight, bmi)
n = bmi
if n<15:
print "You are very severely underweight"
elif 15<=n<=16:
print "You are severely underweight"
elif 16<n<=18.5:
print "You are underweight"
elif 18.5<n<=25:
print "Your weight is normal"
elif 25<n<=30:
print "You are overweight"
elif 30<n<=35:
print "You are moderately obese"
elif 35<n<=40:
print "You are severely obese"
elif n>40:
print "You are very severely obese"
计算bmi时,通常使用身高单位为米(而不是厘米)。这意味着您需要对公式中从cm到m的转换进行两次调整:
bmi = 10000 * weight / (height*height)
print bmi
你可以使用if elif--文本部分的else语句:
if bmi < 15:
message = "very severely underweight"
elif bmi <= 16:
message = "severely underweight"
# Add more elif parts here...
else:
message = "very severely obese"
print "You are", message