初级简单逻辑完成BMI指数函数程序

bodies = {}#存放所有数据

def panDuanHeFa(height,weight):#判断身高体重输入是否合法的函数
    if '.' in height and '.' in weight:
        a, b = height.split('.')
        c, d = weight.split('.')
        if a.isdigit() and b.isdigit() and c.isdigit() and d.isdigit():
            if float(height) > 0.00 and float(height) <= 3.00 and float(weight) > 0.00 and float(weight) <= 200.00:
                return 1
            else:
                return '身高或者体重数值超出合理范围,请重新输入'
        else:
            return '身高或者体重不合法!!!请重新输入'
    else:
        return '身高或者体重不合法!!!请重新输入'

def show():#首页显示的函数
    menus = ("1, 录入", "2, 查询", "3, 删除", "4, 修改", "5, 退出")
    print('*'*15)
    for el  in menus:
        print(el.center(15))
    print('*'*15)

def luru():#循环录入的函数
    while 1:
        dic = {}
        name=input('请输入姓名>>>').replace(' ','')
        height = input('请输入身高(m)至少保留小数点后一位>>>').replace(' ', '')
        weight = input('请输入体重(kg)至少保留小数点后一位>>>').replace(' ', '')
        result=panDuanHeFa(height,weight)
        if result==1:
            dic['name']=name
            dic['height']=float(height)
            dic['weight']=float(weight)
            dic['BMI']=round(float(weight)/(float(height)**2),2)
            if len(bodies)==0:
                bodies.setdefault(1,dic)
                print('录入成功')
            else:
                keys = []
                for key in bodies:
                    keys.append(key)
                xuhao = max(keys) + 1
                bodies.setdefault(xuhao, dic)
                print('录入成功')
        else:
            print(result)
        content=input('是否继续录入?输入Q结束,任意键继续>>>').replace(' ', '')
        if content.upper()=='Q':
            break
        else:
            pass

def find():#查找的函数
    while 1:
        content_id=input('请输入要查询的ID号>>>').replace(' ','')
        if content_id.isdigit():
            content_id=int(content_id)
            if bodies.get(content_id)==None:
                print('不存在此ID')
            elif bodies.get(content_id)=={}:
                print('内容为空')
            else:
                print('姓名:%s  身高:%s m  体重:%s kg BMI:%s '%(
                    bodies[content_id]['name'],bodies[content_id]['height'],
                    bodies[content_id]['weight'],bodies[content_id]['BMI']))
        else:
             print('输入的查询序号不合法')
        content=input('是否继续查询,退出按Q,继续按任意键>>>').replace(' ', '')
        if content.upper()=='Q':
            break
        else:
            pass

def dele():#删除函数
    while 1:
        content_id = input('请输入要删除的ID号>>>').replace(' ','')
        if content_id.isdigit():
            content_id=int(content_id)
            if bodies.get(content_id)==None:
                print('不存在此ID')
            else:
                bodies[content_id]={}
                print('删除成功!!!')
        else:
             print('输入的删除序号不合法')
        content = input('是否继续删除,退出按Q,继续按任意键>>>').replace(' ', '')
        if content.upper() == 'Q':
            break
        else:
            pass

def edit():  #修改函数
    while 1:
        dicc = {}
        content_id = input('请输入要修改的ID号>>>').replace(' ','')
        if content_id.isdigit():
            content_id=int(content_id)
            if bodies.get(content_id)==None:
                print('不存在此ID')
            elif bodies.get(content_id)=={}:
                while 1:  # 判断字符是否合法
                    name = input('请输入修改的姓名>>>')
                    height = input('请输入修改的身高>>>')
                    weight = input('请输入修改的体重>>>')
                    result = panDuanHeFa(height, weight)
                    if result == 1:
                        BMI = round(float(weight) / (float(height) ** 2), 2)
                        dicc['name'] = name
                        dicc['height'] = height
                        dicc['weight'] = weight
                        dicc['BMI'] = BMI
                        bodies[content_id] = dicc
                        print('姓名:%s  身高:%s m  体重:%s kg BMI:%s ' % (
                            bodies[content_id]['name'], bodies[content_id]['height'], bodies[content_id]['weight'],
                            bodies[content_id]['BMI']))
                        break
            else:
                print('姓名:%s  身高:%s m  体重:%s kg BMI:%s ' % (
                bodies[content_id]['name'], bodies[content_id]['height'], bodies[content_id]['weight'],
                bodies[content_id]['BMI']))
                while 1:#判断字符是否合法
                    name=input('请输入修改的姓名>>>')
                    height=input('请输入修改的身高>>>')
                    weight=input('请输入修改的体重>>>')
                    result=panDuanHeFa(height,weight)
                    if result==1:
                        BMI = round(float(weight) / (float(height) ** 2), 2)
                        dicc['name']=name
                        dicc['height']=height
                        dicc['weight']=weight
                        dicc['BMI']=BMI
                        bodies[content_id]=dicc
                        print('姓名:%s  身高:%s m  体重:%s kg BMI:%s ' % (
                            bodies[content_id]['name'], bodies[content_id]['height'], bodies[content_id]['weight'],
                            bodies[content_id]['BMI']))
                        break
                    else:
                        print(result)
        else:
            print('输入的修改序号不合法')
        content = input('是否继续修改,退出按Q,继续按任意键>>>').replace(' ', '')
        if content.upper() == 'Q':
            break
        else:
            pass

def Main():   #主函数
    while 1:
        show()
        chice=input('请输入要执行的操作编号>>>>').replace(' ','')
        if chice=='1':
            luru()
        elif chice=='2':
            find()
        elif chice=='3':
            dele()
        elif chice=='4':
            edit()
        elif chice=='5':
            break
        else:
            print('输入了非法编号,请重新输入!!!!')


Main()

  

posted @ 2018-09-14 20:04  冒蓝火的加特林哒哒哒  阅读(865)  评论(0编辑  收藏  举报