1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python程序设计简明教程课后答案_Python简明教程最后的练习题

python程序设计简明教程课后答案_Python简明教程最后的练习题

时间:2019-06-16 23:12:05

相关推荐

python程序设计简明教程课后答案_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] printelse: 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.' breakelse: print 'You\'ve input a wrong command character,Please input a command again.(The command is one of [add/modify/search/delete/view/quit)]'

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。