Python 练习册

使用知识:文件 、异常处理 、列表 、 序列化数据存储 、反序列化

代码介绍:
简单的宠物管理
信息的增删查改,以及将宠物信息存储在文件中,再次打开使数据不会丢失

目的:练习学习的知识点

#文件读取:把数据读取到全局变量PETS中:

#列表存储数据
PETS=[]
FILE_NAME="PetsInfo.txt"

#执行代码,异常处理
try:
    with open(FILE_NAME,"r",encoding="utf-8") as fp:
        while True:            
            pet_string=fp.readline()
            if not pet_string:
                break
            pet_decode=pet_string.split("|")
            pet_info={"ID":pet_decode[0],"name":pet_decode[1],"category":pet_decode[2],"price":pet_decode[3]}
            PETS.append(pet_info)
        fp.close()
 #发生异常的处理:       
except FileNotFoundError:
    fp=open(FILE_NAME,"w",encoding="utf-8")
    fp.close()


#实现函数:

#添加宠物信息
def add_pet():
    print("add pet infomation:")
    ID=input("请输入宠物编号:")
    name=input("请输入宠物名称:")
    category=input("请输入宠物种类:")
    price=input("请输入宠物价格:")
    temp_pet={"ID":ID,"name":name,"category":category,"price":price}
    PETS.append(temp_pet)
    print("pet infomation insert succeed!")
#查找某个宠物信息
def search_pet():
    print("search pet infomation ****:")
    name=input("请输入宠物名称:")
    flag=1
    for pet in PETS:
        if name==pet["name"]:
            temp="编号:{}\n名称:{}\n种类: {}\n价格: {}".format(
                pet["ID"],
                pet["name"],
                pet["category"],
                pet["price"]
                )
            print(temp)
            flag=2
    if flag==1:
        print("not find pet infomation!")
#删除某个宠物信息
def delete_pet():
    ID=input("请输入宠物的编号")
    for pet in PETS:
        if pet["ID"]==ID:
            PETS.remove(pet)
            print("OK,pet infomation delete succeed!")
            break
#列出所有宠物信息

def list_pet():
    print("list all infomation about pets:")
    for pet in PETS:
        print("-"*30)
        temp="编号: {}\n名称: {}\n种类: {}\n价格: {}".format(
                pet["ID"],
                pet["name"],
                pet["category"],
                pet["price"]
        )
        print(temp)
        print("-"*30)

        

#退出函数
def exit_sys():
    with open(FILE_NAME,"w",encoding="utf-8") as fp:
        lines=[]
        for pet in PETS:
            pet_encode="{}|{}|{}|{}|".format(
                pet["ID"],
                pet["name"],
                pet["category"],
                pet["price"]
            )
            lines.append(pet_encode+"\n")
        fp.writelines(lines)
        fp.close()





#主
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":
            delete_pet()
        elif option=="4":
            list_pet()
        elif option=="5":
            exit_sys()
            break
        else:
            print(u"请输入正确选项:")

main()
        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值