:
#!/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 Beginner exercise 02: Easy Address Book