#python程序练习–简单电话薄
#樊海军
#fanhaijun.com
#Python 2.6.2
import cPickle as p
import string
class phonebook:
def __init__(self):
pblist=[]
def pb_add():
#print "This is the ADD function"
while 1:
print ‘\nPlease input a NEW name["Q" to back to main menu]:\n’
add_name=raw_input("Name:")
if add_name=="Q":
main()
else:
add_sex=raw_input("Sex:")
add_tel=raw_input("Tel:")
pbdict={"Name":add_name,"Sex":add_sex,"Tel":add_tel}
print "You just input:"
for i in pbdict.items():
print ("%s:%-5s ") % (i[0],i[1]),
pb.pblist.append(pbdict)
del pbdict
print ("\nRecord added sucessfully! Continue?(Y/N)\n")
while 1:
con=raw_input()
if con=="Y":
pb_add()
elif con=="N":
pb_save()
main()
else:
print ("\nError! Continue?(Y/N)\n")
def pb_browser():
#print "This is the BROWSER function"
#print pb.pblist
print "\nThere are %s records:\n"% len(pb.pblist)
print "*******************************************"
print " ID NAME SEX TEL"
print "*******************************************"
for i in pb.pblist:
#print "==========================================="
print "%3s " % (pb.pblist.index(i)+1),
print "%-8s" % i["Name"],
print "%-8s" % i["Sex"],
print "%-8s" % i["Tel"]
print "==========================================="
main()
def pb_del():
#print "This is the DEL function"
while 1:
print "\nPlease input the ID of the record you want to delete:[Input 0 to main menu]\n"
del_id=raw_input()
if int(del_id)==0:
main()
elif del_id=="" or int(del_id)<0 or int(del_id)>len(pb.pblist):
#print "\nPlease input a RIGHT ID :\n"
continue
elif int(del_id)<=len(pb.pblist) and int(del_id)>0:
del pb.pblist[int(del_id)-1]
pb_save()
def pb_search():
#print "This is the SEARCH function"
while 1:
print "Please input what ever you want to search:"
s=str(raw_input()).lower()
k=0
id_list=[]
for i in pb.pblist:
if s in i["Name"].lower() or s in i["Sex"].lower() or s in i["Tel"].lower():
k+=1
id_list.append(pb.pblist.index(i))
print "\nThere are %s results:\n"% k
#print id_list
print "*******************************************"
print " ID NAME SEX TEL"
print "*******************************************"
for j in id_list:
print "%3s " % str(j+1),
print "%-8s" % pb.pblist[j]["Name"],
print "%-8s" % pb.pblist[j]["Sex"],
print "%-8s" % pb.pblist[j]["Tel"]
print "==========================================="
print ("\nContinue?(Y/N)\n")
while 1:
con=raw_input()
if con=="Y":
pb_search()
elif con=="N":
main()
else:
print ("\nError! Continue?(Y/N)\n")
def pb_file():
#print "This is the file function"
try:
f=file(pbfile)
pb.pblist=p.load(f)
except:
pb.list=[]
finally:
f.close()
def pb_save():
#print "This is the SAVE function"
try:
f=file(pbfile,"w")
p.dump(pb.pblist,f)
print "\nRecords saved successfully!\n"
finally:
f.close()
def pb_quit():
print "Thanks for your using!"
exit()
def main():
print """\
____________________________________________________
| Please select your choice: |
|__________________________________________________|
| [A]: Add a record. |
| [B]: Browser the records. |
| [D]: Delete a record. |
| [S]: Search. |
| [Q]: Quit. |
|__________________________________________________|
\
"""
while 1:
opp=raw_input()
if opp=="A":
pb_add()
if opp=="B":
pb_browser()
if opp=="D":
pb_del()
if opp=="S":
pb_search()
if opp=="Q":
pb_quit()
pb=phonebook()
pbfile=("pb.dat")
pb_file()
main()
Tag: