电话通讯录

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pickle as p

class person:
    notes={}
    def add(self):
        name = input('请输入要添加的联系人姓名')
        if name in person.notes:
            print('该联系人已经存在')
        else:
          telephone =  (input('请输入联系人电话号码'))
          addr =  (input('请输入联系人地址'))
          label={'电话':telephone,'地址':addr}
          person.notes[name]=label

    def dele(self):
      name = (input('请输入要删除的联系人姓名'))
      if name in person.notes:
        del person.notes[name]
        print ("%s" %  person.notes.items())
      else:
        print('联系人 %s 不存在'%name)

    def search(self):
      name = (input('请输入要搜索的联系人姓名'))
      if name in person.notes:
         print('联系人 %s 的电话号码是 %s , 地址是 %s'%(name,person.notes[name]['电话'],person.notes[name]['地址']))
      else:
         print('联系人 %s 不存在'%name)

    def modify(self):
      name = (input('请输入要编辑的联系人姓名'))
      if name in person.notes:
        telephone =  (input('请输入联系人电话号码'))
        addr =  (input('请输入联系人地址'))
        person.notes[name]['电话']=telephone
        person.notes[name]['地址']=addr
      else:
         print('联系人 %s 不存在,若要编辑请选择添加选项'%name)
    def write(self):
      f = open('联系人.txt','wb+')
      p.dump(person.notes,f)
      f.close()
    def read(self):
      file = '联系人.txt'
      try:
        f = open(file ,'rb+')
        person.notes = p.load(f)
        f.close()
      except:
        f = open(file ,'w')
        f.close()
    def show(self):
      print(person.notes)

def menu():
    print('''系统提供以下功能
    1:添加
    2:删除
    3:修改
    4:搜索
    5: 显示全部联系人信息
    6:退出''')

people = person()
people.read()
while True:
  try:
    menu()
    choice = int(input('请输入相应数字操作'))
    if choice==1:
        people.add()
    elif choice ==2:
        people.dele()
    elif choice ==3:
        people.modify()
    elif choice ==4:
        people.search()
    elif choice==5:
        people.show()
    elif choice ==6:
        people.write()
        break

    else:
        print('输入不合法,请输入合法数字')
  except ValueError:
      print('请输入数字选项')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值