Python 小小联系人

前言

最近一段时间再看python,感觉这个语言真牛逼。
看的简明python教程,挺好的
来一个联系人Demo,用pickle本地保存数据。
直接上代码,注释很多,有兴趣的可以敲敲,语言很简洁.

#!/usr/bin/python

import pickle as p

import time
import sys

#通讯录制作


#用户类
class Person:
    #创建
    def __init__(self,name,phone):
        self.name = name
        self.phone = phone

    #输出信息
    def print(self):
        print("name : %s,age : %s" % (self.name,self.phone))

    #修改
    def update(self,name,phone):
        self.name = name
        self.phone = phone




if __name__ == '__main__':

    # 存储位置
    contactsfile = "contacts.data"

    #获取存储的数据
    def obtainData():
        try:
            f = open(contactsfile, "rb+")
            mylist = p.load(f)
            f.close()
            return mylist
        except Exception:
            return []

    #保存数据
    def saveData(person):
        #首先判断本地是否已经有这个名字的数据
        mylist = obtainData()
        flag = False
        #判断本地是否油值
        if len(mylist) != 0:
           for local in mylist:
               if local.name == person.name:
                   flag = True
                   print("error,local has the one contacts")
                   break

        if flag != True:
            #说明本地没有这个人
            mylist.append(person)
            try:
                f = open(contactsfile, "wb+")
                p.dump(mylist, f)
                f.close()
                print("save success")
            except Exception:
                print("save failure")

    #搜索
    def searchDataByName(name,showPrint):
        mylist= obtainData()
        flag = False
        if len(mylist) != 0:
            for person in mylist:
                if person.name == name:
                    if showPrint:
                        print("find this person,name :%s ,phone :%s" % (person.name, person.phone))
                    flag = True
                    break

        if flag != True:
            if showPrint:
                print("sorry,contacts no this name")

        return flag

    #更新
    def updateData(name,phone):
        mylist = obtainData()
        flag = False
        if len(mylist) > 0:
            for person in mylist:
                if person.name == name:
                    person.update(name,phone)
                    flag = True
                    break


        if flag == True:
            f = open(contactsfile,"wb+")
            p.dump(mylist,f)
            f.close()
            print("update success")

    #删除
    def deleteData(name):
        mylist = obtainData()
        if len(mylist) != 0:
            for person in mylist:
                if person.name == name:
                    mylist.remove(person)
                    f = open(contactsfile,"wb+")
                    p.dump(mylist,f)
                    f.close()
                    print("delete success")
                    return




    while True:
        # 开始处理
        print("\n\n\n=====================欢迎使用通讯录=================================")
        print("操作菜单:1 查看   2 添加    3 更新   4 删除    5 搜索  q 退出")

        s = input("\nEnter Choose:")
        if s == '1':
            #查看
            mylist = obtainData()
            if len(mylist) > 0:
                for person in mylist:
                    person.print()
            else:
                print("local no contacts")

        elif s == '2':
            #添加
            s = input("Please person message(style:name*phone):")
            #处理输入
            inputlist = s.split("*")
            #判断手机是否符合
            if len(inputlist[1]) != 11:
                print("sorry,phone format error")
            else:
                person = Person(inputlist[0], inputlist[1])
                saveData(person)
        elif s == "3":
            #更新
            s = input("Please input update name:")
            flag = searchDataByName(s,False)
            if flag == True:
                #说明本地有这个数据
                s2 = input("Please input update phone:")
                if len(s2) != 11:
                    print("sorry,phone format error")
                else:
                    updateData(s,s2)
            else:
                print("sorry,this name is not in local")

        elif s == "4":
            #删除
            s = input("Please input delete name:")
            deleteData(s)
        elif s == "5":
            #搜索
            s = input("Please input search name:")
            searchDataByName(s,True)
        elif s =='q':
            #退出
            print("see you again")
            sys.exit()
        else:
            print("choose error,please again")

你可能感兴趣的:(Python 小小联系人)