Python初学练习02:简易通讯录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python初学练习02:简易通讯录相关的知识,希望对你有一定的参考价值。
效果图:
#!/usr/bin/env python
import tab,os,sys
exitcheck = False
listfile = ‘Addresslist.data‘
Dictionary = {}
#with open(listfile,‘a‘) as datafile
datafile=file(listfile,‘r‘)
datafile.seek(0)
for line in datafile.readlines():
id=line.split()[0]
name=line.split()[1]
phone=line.split()[2]
email=line.split()[3]
Dictionary[id]=(name,phone,email)
#print Dictionary
while exitcheck is False:
print "Welcome AddressList!"
print " 1#Display ALL List"
print " 2#Find Item"
print " 3#Addend Item"
print " 4#Dell Item"
print " 5#exit"
InputOption=int(raw_input("Please Input option[1-5]:").strip())
if InputOption == 1:
print "1#Display ALL List"
while InputOption != "q":
page=0
for key in Dictionary.keys():
print key,Dictionary[key][0],Dictionary[key][1],Dictionary[key][2]
if page == 4 :
page=0
InputOption = raw_input("q:break anyone:next").strip()
if InputOption == "q": break
else:page=page+1
if InputOption != "q":
InputOption = raw_input("q:break anyone:restart").strip()
elif InputOption == 2:
while InputOption != "q":
print "2#Find Item"
print "1_name"
print "2_phone"
print "3_mail"
findkeyid = ‘Null‘
InputOption = raw_input("Option[q:break findItem[1-3]").strip()
if InputOption == "1":
findinput = raw_input("Input Find key(name):").strip()
for id in Dictionary.keys():
if Dictionary[id][0]==findinput:
print id,Dictionary[id][0],Dictionary[id][1],Dictionary[id][2]
findkeyid=id
if InputOption == "2":
findinput = raw_input("Input Find key(phone):").strip()
for id in Dictionary.keys():
if Dictionary[id][1]==findinput:
print id,Dictionary[id][0],Dictionary[id][1],Dictionary[id][2]
findkeyid=id
if InputOption == "3":
findinput = raw_input("Input Find key(Email):").strip()
for id in Dictionary.keys():
if Dictionary[id][2]==findinput:
print id,Dictionary[id][0],Dictionary[id][1],Dictionary[id][2]
findkeyid=id
if findkeyid !=‘Null‘:
InputOption = raw_input("Option[d:del. e:edit. anyone:break")
if InputOption == "d":
InputOption =raw_input("delete it yes or no?").strip()
if InputOption =="y":
del Dictionary[findkeyid]
print "Del success!"
InputOption = ‘‘
elif InputOption =="e":
#print findkeyidi
print findkeyid+Dictionary[findkeyid][0]+" "+Dictionary[findkeyid][1]+" "+Dictionary[findkeyid][2]
nkeyid=findkeyid
nname=raw_input("NewName:").strip()
nphone=raw_input("NewPhone:").strip()
nemail=raw_input("NewEmail:").strip()
if len(nkeyid)>0:
Dictionary[nkeyid]=(nname,nphone,nemail)
print "Item Edit success! "
#InputOption = "q"
else:
print "Not Find Item"
elif InputOption ==3:
while InputOption != "q":
print "3#Addend Item"
nkeyid=""
nname=""
nphone=""
nemail=""
while len(nkeyid) == 0 or len(nname) == 0 :
nkeyid=raw_input("ID:").strip()
nname=raw_input("Name:").strip()
nphone=raw_input("Phone:").strip()
nemail=raw_input("Email:").strip()
if len(nkeyid) > 0 and len(nname) > 0 :
for keyid in Dictionary.keys():
if keyid == nkeyid :
print "ID is Repeat."
nkeyid=""
break
if len(nkeyid)>0:
Dictionary[nkeyid]=(nname,nphone,nemail)
print "Item Add success! "
InputOption = "q"
else:
print "ID or name is Null."
elif InputOption ==4:
while InputOption != "q":
print "4#Dell Item"
delkeyid=raw_input("DELETE ID:").strip()
if len(delkeyid) > 0:
del Dictionary[delkeyid]
print "Del success!"
InputOption = ‘q‘
elif InputOption ==5:
print "5#Exit"
savefile = file(listfile,‘w‘)
for key in Dictionary.keys():
str = key + ‘ ‘ + Dictionary[key][0] + ‘ ‘ + Dictionary[key][1] + ‘ ‘ + Dictionary[key][2] + ‘\n‘
savefile.write(str)
savefile.close
break
以上是关于Python初学练习02:简易通讯录的主要内容,如果未能解决你的问题,请参考以下文章