python简明教程练习--命令行 地址簿 程序

在这个程序中,你可以添加、修改、删除和搜索你的联系人(朋友、家人和同事等等)以及它们的信息(诸如电子邮件地址和/或电话号码)。这些详细信息应该被保存下来以便以后提取。

由于上个程序中的代码没有把信息从文件中读取出来,我们可以把相同的信息保存在info.txt文件中去,这使得info.txt文件会有多余的东西。以下是我修改后的代码

代码:

'''

Created on 2011-10-16

@author: Descusr

'''

# coding:utf-8 

import sys

import os

import cPickle

class Person:

    filename = 'info.txt'

    def __init__(self, name, phone):

        self.name = name

        self.phone = phone

        self.personlist = {self.name:self.phone}

        

    def add(self):

        self.name = raw_input("Enter name:")

        if self.name in self.personlist.keys():

            print "The name is exist!"

        else:

            self.phone=raw_input("Enter phone:")

            self.personlist[self.name] = self.phone

            print "Contact saved!"

            

    def Modify(self):

        self.name = raw_input("Enter name:")

        if self.name in self.personlist.keys():

            self.phone = raw_input("Enter new phone:")

            self.personlist[self.name] = self.phone

            print "Contact saved!"

        else:

            print "The name is not in here!!"

            

    def delete(self):

        self.name = raw_input("Enter name:")

        if self.name in self.personlist.keys():

            del self.personlist[self.name]

            print "Deleted!!"

        else:

            print "The name is not in here!!"

    

    def search(self):

        self.name = raw_input("Enter name:")

        if self.name in self.personlist.keys():

            print 'Name:%s,Phone:%s'%(self.name,self.phone)

        else:

            print "The name is not in here!!"

     

    def save(self):

        f = file(self.filename,'w')

        cPickle.dump(self.personlist,f)

        f.close()

        print "Your contacts list has been saved to file:%s successfully~" %self.filename

        

    def load(self):

        if os.path.exists(self.filename):

            f = file(self.filename)

            self.personlist = cPickle.load(f)

            f.close()

            

    def show(self):

        for self.name,self.phone in self.personlist.items():

            print "Name:%s  Phone number:%s" %(self.name,self.phone)

            

if __name__ == '__main__':

    os.system('clear')

    command = ['add','modify','search','delete','quit' ,'show']

    person = Person('','')

    person.load()

    while True:

        print "The contact person:"

        str = raw_input('What are you going to do(add/modify/search/delete/show/quit)?')

        if str in command:

            if str == 'add':

                person.add()

            elif str == 'modify':

                person.Modify()

            elif str == 'search':

                person.search()

            elif str == 'delete':

                person.delete()

            elif str == 'show':

                person.show()

            else:

                ch = raw_input("Your contacts list hasn't  been saved,save it now?(Y/N)")

                if ch == 'y':

                    person.save()

                    sys.exit()

                else:

                    sys.exit()

                print "Exit the System"

                break

        else:

            print "Please input the command!"

不足之处,请批评指正!

posted @ 2011-10-15 11:52  网恋被骗5元  阅读(1715)  评论(2编辑  收藏  举报