写个Python(一)

通过学习人生苦短,我学Python(二)中记录的学习内容,我自己写了一个简单的计算BMI值的小程序。程序的具备输入/输出,简单来说就是,输入身高和体重数据,程序返回BMI值和肥胖程度,第一次写的代码如下(貌似丑陋臃肿至极):

inputHeight = input('Please input your height(CM):')
inputWeight = input('Please input your weight(KG):')
height = float(inputHeight)
weight = float(inputWeight)

BMI = weight / ((height / 100) ** 2)

if BMI >= 32:
    print('Your weight is:', weight, 'KG, ', 'and height is:', height, 'CM。')
    print('Your BIM is:', round(BMI, 2), 'Your body is:', 'Severe obesity')
elif BMI >= 28:
    print('Your weight is:', weight, 'KG, ', 'and height is:', height, 'CM。')
    print('Your BIM is:', round(BMI, 2), 'Your body is:', 'Obesity')
elif BMI >= 25:
    print('Your weight is:', weight, 'KG, ', 'and height is:', height, 'CM。')
    print('Your BIM is:', round(BMI, 2), 'Your body is:', 'Overweight')
elif BMI >= 18.5:
    print('Your weight is:', weight, 'KG, ', 'and height is:', height, 'CM。')
    print('Your BIM is:', round(BMI, 2), 'Your body is:', 'Normal')
else:
    print('Your weight is:', weight, 'KG, ', 'and height is:', height, 'CM。')
    print('Your BIM is:', round(BMI, 2), 'Your body is:', 'Underweight')

好像,一点都不简洁,一点都不优雅...于是我把它改了改,如下:

#定义BMIcalculator函数用于计算BMI,传入height和weight,输出BMI值。
def BMIcalculator(height, weight):
    BMI = round(weight / ((height / 100) ** 2), 2)
    return BMI
#定义judgeBMI函数用于判断肥胖程度,传入BMI,输出肥胖程度字符串。
def judgeBMI(BMI):
    if BMI >= 32:
        return 'Severe obesity'
    elif BMI >= 28:
        return 'Obesity'
    elif BMI >= 25:
        return 'Overweight'
    elif BMI >= 18.5:
        return 'Normal'
    else:
        return 'Underweight'
#输入height和weight的值,并将其转化为浮点数,便于计算。
height = float(input('Please input your height(CM):'))
weight = float(input('Please input your weight(KG):'))
print('Your BMI is:', BMIcalculator(height, weight))
print('Your body is:', judgeBMI(BMIcalculator(height, weight)))

额,貌似优雅了不少。改动中,将整个程序的功能分成了两块,第一部分就是函数BMIcalculator(height, weight),用于根据身高、体重计算出BMI值。第二部分,即函数judgeBMI(BMI),用于利用BMI值判断肥胖程度,并输出结果。

以上两个部分在目前其实是彼此分离的,包括第一个函数,也需要输入heightweight两个参数才能返回BMI,所以我们接下来需要做的就是输入参数,调用函数BMIcalculator(height, weight),得到BMI值,再将返回的BMI值作为参数调用函数judgeBMI(BMI),最后得肥胖程度并打印出来。我用下面的几行代码来实现了上述的功能,其实核心就是用第一个函数的返回值做为第二个函数的参数调用第二个函数:

height = float(input('Please input your height(CM):'))
weight = float(input('Please input your weight(KG):'))
print('Your BMI is:', BMIcalculator(height, weight))
print('Your body is:', judgeBMI(BMIcalculator(height, weight)))

额,这次就先到这儿,关于这个BMI计算的程序,我会利用我在接下来学习到的东西来不断地优化。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,524评论 0 17
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,948评论 18 139
  • 1 spinoza的哲学基础God and the processes of nature are one.上帝和...
    梦小汐阅读 173评论 0 0
  • 当你忙碌的时候,你的思想里再也放不下其他的东西。忙碌其实也是一种自我放逐的方式。
    一米阳光12阅读 98评论 0 0
  • 早该明白“ 如果”都是假的 我考上了 我可以和我的朋友 在未来 继续过着忙碌的日子 可能会很苦很累很折磨 可是 我...
    QkiayeuS_阅读 214评论 0 0