Python簡明教程最後的練習題

作爲一個菜鳥, 沒點進步都是值得記錄的. 嘿嘿, 今天重新看了一遍Python簡明教程, 終於獨立完成了最後的練習(老鳥勿笑). 特粘貼代碼在下面,希望大家指導.微笑



import cPickle




class Person:
    '''Represents a person,which includes name, phone, email and so on.'''
    phonebook = {}              #Create a dict for storing the contact
    def __init__(self):
        self.name = ''
        self.phone = ''
        self.email = ''
        
    def add(self):
        '''Add a contact.'''
        self.name = raw_input('Please input a name: ')
        if Person.phonebook.has_key(self.name):
            print 'The name is exist!'
        else:
            self.phone = raw_input('Please input the phone: ')
            self.email = raw_input('Please input the email: ')
            Person.phonebook[self.name] = self.phone + ' ; ' + self.email
            try:
                cPickle.dump(Person.phonebook, file('phoneData.txt', 'w'))
                print 'Contact saved.'
            except IOError as ioerr:
                print 'File error:(add)' + str(ioerr)
                
        




    def modify(self):
        '''Modify a contact's information.'''
        self.name = raw_input('Please input the name you want to modify: ')
        if Person.phonebook.has_key(self.name):
            self.phone = raw_input('Please input the phone: ')
            self.email = raw_input('Please input the email: ')
            try:
                cPickle.dump(Person.phonebook, file('phoneData.txt', 'w'))
                print 'Contact saved.'
            except IOError as ioerr:
                print 'File error:(modify)' + str(ioerr)
        else:
            print 'There\'s no one named %s,please confirm your input.' %self.name
        




    def delete(self):
        '''Delete a contact's information.'''
        self.name = raw_input('Please input the name you want to delete: ')
        if Person.phonebook.has_key(self.name):
            del Person.phonebook[self.name]
            try:
                cPickle.dump(Person.phonebook, file('phoneData.txt', 'w'))
                print 'Contact saved.'
            except IOError as ioerr:
                print 'File error:(delete)' + str(ioerr)
        else:
            print 'There\'s no one named %s,please confirm your input.' %self.name
        
            
    def search(self):
        '''Search and show a contact's information.'''
        Person.phonebook = cPickle.load(file('phoneData.txt'))
        self.name = raw_input('Please input the name you want to search: ')
        if Person.phonebook.has_key(self.name):
            print
            print 'Name: %s' %self.name
            print 'Phoen&Email: %s' %Person.phonebook[self.name]
            print
        else:
            print 'There\'s no one named %s,please confirm your input.' %self.name




    def view(self):
        '''Show all contacts's information.'''
        try:
            Person.phonebook = cPickle.load(file('phoneData.txt'))
        except IOError as ioerr:
            print 'File error:(view)' + str(ioerr)
        for self.name,self.phone in Person.phonebook.items():
            print 
            print 'Name: %s,\nPhone&Email: %s' %(self.name, self.phone)
            print 




if __name__ == '__main__':
    p = Person()
    print '''Hello,I'm you phonebook guide,you can input "add" to add a person's
              information,"modify" to modify a contact, "delete" to delete a contact, "search" to search person's
              information, "quit" to quit and exit system,"view" to show all contacts information'''
    while True:
        inputChar = raw_input('What are you going to do(add/modify/search/delete/view/quit)? \n>>')
    
        if inputChar == 'add':
            p.add()
        elif inputChar == 'modify':
            p.modify()
        elif inputChar == 'delete':
            p.delete()
        elif inputChar == 'search':
            p.search()
        elif inputChar == 'view':
            p.view()
        elif inputChar == 'quit':
            print 'Exit the system.'
            break
        else:
            print 'You\'ve input a wrong command character,Please input a command again.(The command is one of [add/modify/search/delete/view/quit)]'
    


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章