1. 程式人生 > >BMI指數函數程序

BMI指數函數程序

oat 主函數 查詢 HICE res 判斷 git split 體重

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()

  

BMI指數函數程序