可以將數(shù)據(jù)存儲在列表里
創(chuàng)新互聯(lián)公司2013年成立,先為中方等服務(wù)建站,中方等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為中方企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
然后再將列表保存到文件里
通過對列表的操作動態(tài)更改存儲在文件里的內(nèi)容
如果有文件的話程序啟動時從文件里加載數(shù)據(jù)保存到列表即可
如果代碼不想自己寫的話
我可以有償代勞
python手機(jī)通訊錄封裝方法:
1、用戶輸入1時,執(zhí)行增加姓名和手機(jī)號碼的操作。
2、選擇字典,轉(zhuǎn)換為元組放在列表里保存break即可。
要求提的不是很明確,按照最簡單的功能來做了。
只記錄姓名和電話號碼,沒有做號碼檢驗。也沒有做保存功能。
import?sys
last_id?=?0
class?Person:
def?__init__(self,?name,?phone):
self.name?=?name
self.phone?=?phone
global?last_id
last_id?+=?1
self.id?=?last_id
def?match(self,?filter):
return?filter?in?self.name?or?filter?in?self.phone
class?AddressBook:
def?__init__(self):
self.people?=?[]
def?new_person(self,?name,?phone=''):
self.people.append(Person(name,?phone))
def?find_person(self,?id):
for?person?in?self.people:
if??str(id)?==?str(person.id):
return?person
return?None
def?modify_person(self,?id,?name,?phone):
person?=?self.find_person(id)
if?person:
person.name?=?name
person.phone?=?phone
else:
print?"person:",?id,?"?not?found"
def?delete_person(self,?id):
person?=?self.find_person(id)
if?person:
self.people.remove(person)
def??search(self,?filter):
return?[person?for?person?in?self.people?if?person.match(filter)]
class?Menu:
def?__init__(self):
self.address_book?=?AddressBook()
self.choices?=?{
"1":?self.show_people,
"2":?self.add_person,
"3":?self.remove_person,
"4":?self.modify_person,
"5":?self.search_person,
"6":?self.quit
}
def?display_menu(self):
print("""
Address?Book?Menu
1.?Show?all?People
2.?Add?Person
3.?Remove?Person
4.?Modify?Person
5.?Search?Person
6.?Quit
""")
def?run(self):
while?True:
self.display_menu()
choice?=?raw_input("Enter?an?option:?")
action?=?self.choices.get(choice)
if?action:
action()
else:
print("{0}?is?not?a?valid?choice".format(choice))
def?show_people(self,?people=None):
if?not?people:
people?=?self.address_book.people
for?person?in?people:
print("{0}:?{1}?{2}".format(person.id,?person.name,?person.phone))
def?search_person(self):
filter?=?raw_input("search?for:?")
people?=?self.address_book.search(filter)
self.show_people(people)
def?add_person(self):
name?=?raw_input("Enter?a?name:?")
phone?=?raw_input("Enter?his/her?phone?number:?")
self.address_book.new_person(name,?phone)
print("{0}?has?been?added.".format(name))
def?remove_person(self):
id?=?raw_input("Enter?his/her?id:?")
if?id:
self.address_book.delete_person(id)
def?modify_person(self):
id?=?raw_input("Enter?a?person?id:?")
name?=?raw_input("Enter?his/her?name:?")
phone?=?raw_input("Enter?his/her?phone:?")
self.address_book.modify_person(id,?name,?phone)
def?quit(self):
print("Thank?you?for?using?your?address?book?today.")
sys.exit(0)
Menu().run()