Discuss / Python / 自作聪明进行小改动的作业~~

自作聪明进行小改动的作业~~

Topic source
# -*- coding: utf-8 -*-

height=input('Please enter your height:')
weight=input('Please enter your weight:')
h=float(height)
w=float(weight)
bmi=w/h/h

if bmi<18.5:
    print('您的BMI为%.1f,过轻' %bmi)
elif bmi<25:
    print('您的BMI为%.1f,正常' %bmi)
elif bmi<28:
    print('您的BMI为%.1f,过重' %bmi)
elif bmi<32:
    print('您的BMI为%.1f,肥胖' %bmi)
else:
    print('您的BMI为%.1f,严重肥胖' %bmi)

最开始转化的时候还是写的

h=int(height)
w=int(weight)

然后报错

Traceback (most recent call last):
  File "bmi.py", line 5, in <module>
    h=int(height)
ValueError: invalid literal for int() with base 10: '1.75'

之前没有仔细看错误信息,看了之后发现是数字类型不匹配,遂改之,现在就可以随便输入进行计算啦~~ 学习使我快乐 (≖ ‿ ≖)✧

对啦,最后加pass,这个用法好像没看到讲解诶~难道是我眼花漏看了?

ಥ_ಥ

%.if 的用法受教了


  • 1

Reply