python创建你自己的命令行,地址簿程序



import cPickle as P
import os


class Contact:   #define a class of contact person ,including name ,telnum,email
   
    def __init__(self,name,telnum,mailaddr):
        self.name=name
        self.telnum=telnum
        self.mailaddr=mailaddr
    def display(self):
        print '%s \'s telnum is %s and the addr is %s' %(self.name ,self.telnum, self.mailaddr)

p=Contact('jemmy','48844','shanghai@163.com')
p2=Contact('Tommy','77777','hangzhou@163.com')


notebook={p.name : p,      # creat a original dictionary include two person
          p2.name:p2}

print'the original notebook was as follows'
for name ,p3 in notebook.items():
           
            print '%s \'s telnum is %s and the addr is %s' % (name,p3.telnum,p3.mailaddr) #$$$$$  display the dict which is
                                                                                          #$$$$$  consisted of a Class

if not os.path.isfile('dictnote.txt'):       #if the code is to run for the first time ,then creat the diction file ,
    dictfile = file('dictnote.txt','w')      # copy the original two person to the file
    P.dump(notebook,dictfile)
    dictfile.close()  #need to close the file before the opreration of (P.load)

 #for test  #  notebook= P.load(dictfile)
            # for name ,p3 in notebook.items():
            #      print '%s \'s telnum is %s and the addr is %s' % (name,p3.telnum,p3.mailaddr)
            # dictfile.close()  
  
else:                                         #if the diction file has already exist ,copy the content to the notebook
   dictfile=file('dictnote.txt')
   notebook=P.load(dictfile)
   dictfile.close()
                               # give user some commonds to operate the notebook
running = True
while running:
    commond=raw_input('inquiry(I),displayall(P),addone(A),delone(D),modify(M),exit(E)\n')
    if commond=='P':
        for name ,p3 in notebook.items():
           
            print '%s \'s telnum is %s and the addr is %s' % (name,p3.telnum,p3.mailaddr)
        continue    
    if commond=='I':
        while True:
           comname=raw_input('whose information you need,press q to quit\n')
           if comname in notebook:
                                                             #$$$$$$ display the content of object in the notebook
              print '%s \'s telnum is %s and the addr is %s' %(comname,notebook[comname].telnum,notebook[comname].mailaddr)
              break
           elif comname=='q':
              break
             
           else:                           
              print 'the name wasnt in the notebook,try again'
             
        continue
    if commond=='A':
         addname=raw_input('add a person\'s name')
         addtel=raw_input('add a person\'s telnum')
         addaddr=raw_input('add a person\'s mailaddr')
         addPer=Contact(addname,addtel,addaddr)
         notebook[addPer.name]=addPer                       #$$$$ opreation of adding a key and a value of (Class)
         if addname in notebook:
            print '%s \'s information add successfully ' %addname
         continue
    if commond=='D':
        while True:          
            delname=raw_input('whose information do you want to delete\n')
            if delname in notebook:
                break
            elif delname=='q':
                break
            else:
                print 'the name wasnt in the notebook,try again'
                continue
        if delname=='q':
             continue
        del notebook[delname]                               #$$$$$ delete a key
        if delname not in notebook:                     
           print '%s \'s information del successfully '%delname
           continue
    if commond=='M':                                        #modify a key
        while True:
           
              modname=raw_input('whose information do you want to modify,press q to quit\n')
              if modname in notebook:
                  break
              elif modname=='q':
                  break
              else:
                  print 'the name wasnt in the notebook,try again'
                  continue
        if modname=='q':
           continue
        modtel=raw_input('input the new telnum')
        modaddr=raw_input('input the new mailaddr')
        if modname in notebook:
            notebook[modname].telnum=modtel
            notebook[modname].mailaddr=modaddr
            print '%s \'s information modify successfully '%modname
            continue
    if commond=='E':
        break
    else:
        print'It\'s an illeagal commond ,please input again' 
        continue
dictfile = file('dictnote.txt','w')     #save the new notebook to the dictfile.txt
P.dump(notebook,dictfile)
dictfile.close()

dictfile=file('dictnote.txt')             #display  the content of the dictfile.txt
notebook= P.load(dictfile)
for name ,p3 in notebook.items():
        print '%s \'s telnum is %s and the addr is %s' % (name,p3.telnum,p3.mailaddr)
dictfile.close()



       

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值