美文网首页
练手项目 宠物管理系统

练手项目 宠物管理系统

作者: 无敌帅帅头 | 来源:发表于2019-01-21 16:36 被阅读0次

    PETS= []

    FILENAME ='pets.txt'

    try:

        with open(FILENAME, 'r', encoding='utf-8')as fp:

            for linein fp:

                info = line.split("/")

                ID = info[0]

                name = info[1]

                categary = info[2]

                price = info[3]

                PETS.append({'id':ID,'name':name,'categary':categary,'price':price})

    except FileNotFoundError:

        fp =open(FILENAME,'w',encoding='utf-8')

        fp.close()

    def add_pet():

        Id =input('请输入宠物编号:')

        name =input('请输入宠物名:')

        categary =input('请输入宠物名称:')

        price =input('请输入宠物价格:')

        pet = {'id':Id,'name':name,'categary':categary,'price':price}

        PETS.append(pet)

        print('恭喜你添加成功')

    def search_pet():

        name =input('请输入宠物的名字: ')

        for petin PETS:

            if name == pet['name']:

                test ="编号:{},姓名:{},名称:{},价格:{}".format(

                    pet['id'],

                    pet['name'],

                    pet['categary'],

                    pet['price']

    )

    print(test)

    def del_pet():

        pet_id =input('请输入要删除宠物的编号:')

        for petin PETS:

        if pet_id == pet['id']:

            PETS.remove(pet)

            print('删除成功')

            break

    def list_pet():

        for petin PETS:

            test ="编号:{},姓名:{},名称:{},价格:{}".format(

                pet['id'],

                pet['name'],

                pet['categary'],

                pet['price']

    )

    print(test)

    def exit_program():

        with open(FILENAME,'w',encoding='utf-8')as fp:

        lines = []

        for pet in PETS:

            test ="{ID}/{name}/{categary}/{price}".format(#格式化成字符串才可以保存

                    ID = pet['id'],

                    name = pet['name'],

                    categary = pet['categary'],

                    price = pet['price']

    )

            print(test)

            lines.append(test+'\n')

    fp.writelines(lines)

    def main():

        print('='*30)

        print('1.添加宠物')

        print('2.查找宠物')

        print('3.删除宠物')

        print('4.列出宠物')

        print('5.退出程序')

        print('='*30)

        while True:

            option =input('请输入序号:')

            if option =='1':

                add_pet()

            elif option =='2':

                    search_pet()

            elif option =='3':

                    del_pet()

            elif option =='4':

                    list_pet()

            elif option =='5':

                    exit_program()

                    break

            else:

                print('请输入正确的选项')

    main()

    相关文章

      网友评论

          本文标题:练手项目 宠物管理系统

          本文链接:https://www.haomeiwen.com/subject/cigcjqtx.html