码迷,mamicode.com
首页 > 编程语言 > 详细

Python初学练习02:简易通讯录

时间:2016-11-04 02:47:32      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:python

效果图:


#!/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:简易通讯录

标签:python

原文地址:http://loren2012.blog.51cto.com/5089992/1869037

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!