您的位置:首页 > 编程语言 > Python开发

Python简明教程最后的练习题

2012-10-06 16:20 309 查看
作为一个菜鸟, 没点进步都是值得记录的. 嘿嘿, 今天重新看了一遍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)]'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: