02 地址簿程序

  • # coding=gbk
    # 问题:  编写一款你自己的命令行地址簿程序,你可以用它浏览、添加、编辑、删除或搜索你的
    #         联系人,例如你的朋友、家人、同事,还有他们诸如邮件地址、电话号码等多种信息。
    #         这些详细信息必须被妥善储存以备稍后的检索。
    # 1. load函数里面必须用global指明addre_book是全局变量,否则不知道为什么会默认为局部变量
    # 2. 有一个更好的实现方法,只是暂时没兴趣重写一遍了,具体如下:
    #    创建一个类用来表示人的信息。使用一份字典来存储人物对象,将它们的名字当作键
    #    值。使用 pickle 模块来将对象长久地存储在硬盘上。使用字典的内置方法来添加、删除
    #    或编辑地址簿中的人物。
    import pickle
    address_book = [("奥斯卡", "我自己", "214231@qq.com", "13578666755"),
                    ("谭大神", "我的室友", "223423@qq.com", "13577863432"),
                    ("小王", "我的儿子", "2251232@qq.com", "13421424432")]
    def save():
        f = open("address_book_file", "wb")
        pickle.dump(address_book, f)
        f.close()
    def load():
        global address_book
        f = open("address_book_file", "rb")
        address_book = pickle.load(f)
        f.close()
    def browser():
        for i in address_book:
            print(i)
    def add(name, note, email, tel):
        address_book.append((name, note, email, tel))
    def search(info):
        for name, note, email, tel in address_book:
            if name.find(info)!=-1 or note.find(info)!=-1 or email.find(info)!=-1 or tel.find(info)!=-1:
                print((name, note, email, tel))
    def delete(name):
        for i in address_book:
            if i[0] == name:
                address_book.remove(i)
    def edit(search_name):
        for name, note, email, tel in address_book:
            if name == search_name:
                print((name, note, email, tel))
                address_book.remove((name, note, email, tel))
                while True:
                    get_input = input("请输入要修改的key:name, note, email, tel: ")
                    get_value = input("请输入要修改的值: ")
                    if get_input=="name":
                        name = get_value
                        break
                    elif get_input=="note":
                        note = get_value
                        break
                    elif get_input=="email":
                        email = get_value
                        break
                    elif get_input=="tel":
                        tel = get_value
                        break
                    else:
                        print("您输入的key有问题:")
                address_book.append((name, note, email, tel))
                break
    
    
    
    flag = True
    while flag:
        get_input = input("你想做什么,b浏览,a添加,e编辑,d删除,s搜索,S保存, L加载,q退出: ")
        if get_input == 'b':
            browser()
        elif get_input == 'a':
            name = input("name: ")
            note = input("note: ")
            email = input("email: ")
            tel = input("tel: ")
            add(name, note, email, tel)
        elif get_input == 'e':
            name = input("name: ")
            edit(name)
        elif get_input == "d":
            name = input("name: ")
            delete(name)
        elif get_input == "s":
            info = input("info: ")
            search(info)
        elif get_input == "q":
            flag = False
        elif get_input == "S":
            save()
        elif get_input == "L":
            load()
        else:
            print("请输入正确的命令: ")

     

posted on 2018-04-14 10:55  jkn1234  阅读(360)  评论(0编辑  收藏  举报