选题二:通讯录是记录了联系人姓名和联系方式的名录,人们可以在通讯录中通过姓名查看相关联系人的联系方式等信息,也可以在其中新增联系人,或修改、删除联系人信息。要求实现手机通讯录程序,该程序中具备添加联系人、查看/修改联系人信息以及删除联系人功能。(参考书中第5章)
这是本人在本学期的考核作业,在文章最后有源代码全文,如有需要,请自取。
#1.对手机号码和邮箱的判断
这是两段基于原题上所添加的功能,参考于他人,改动之后适用于txt形式
#2.添加联系人
#3.删除联系人
#4.查找联系人
#5.修改联系人
#6.打印通讯录
本文采用txt形式编写”手机通讯录“,其数据在“通讯录.txt”显示,而文件采用绝对地址,如有需求,需自行对绝对地址进行更改,之后是源代码全文。
import json import sys import time addressBook = [] # 通讯录,用于存放联系人信息 class Tel: def Show(self):#显示目录 print("*"*49) print("*\t\t\t\t1.添加联系人\t\t\t\t\t\t*") print("*\t\t\t\t2.删除联系人\t\t\t\t\t\t*") print("*\t\t\t\t3.查找联系人\t\t\t\t\t\t*") print("*\t\t\t\t4.修改联系人信息\t\t\t\t\t*") print("*\t\t\t\t5.打印通讯录\t\t\t\t\t\t*") print("*\t\t\t\t0.退出系统\t\t\t\t\t\t*") print("*"*49) def check_phone(self, tel):#判断电话号码的正确性 # 手机号合法性判断,这里只判断了号段(前三位) import re pattern = re.compile(r'^(13[0-9]|14[0|5|6|7|9]|15[0|1|2|3|5|6|7|8|9]|' r'16[2|5|6|7]|17[0|1|2|3|5|6|7|8]|18[0-9]|' r'19[1|3|5|6|7|8|9])\d{8}$') if pattern.search(tel): return tel # 返回合法的手机号 else: tel = input("手机号输入有误,请重新输入:") return self.check_phone(tel) # 重复进行合法性判断,并返回合法的手机号 def check_email(self,mail): # 邮箱合法性判断 import re pattern = re.compile(r'^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$') if pattern.search(mail): return mail # 返回合法的邮箱 else: mail = input("邮箱输入有误,请重新输入:") return self.check_email(mail) # 重复进行合法性判断,并返回合法的邮箱 def Add(self):#1.添加联系人 while 1: name = input("\t请输入联系人的姓名:") if name.strip(): # 判断输入是否为空 for i in addressBook: if name in i['姓名']: # 判断好友是否已经存在 print("该好友已经存在!") return # 好友已经存在,跳出add_contacts()函数 break # 好友尚未存在,跳出while循环 else: print("输入为空,请输入正确信息!") adr=input("\t请输入联系人地址:") tel=input("\t请输入联系人电话:") tel = self.check_phone(tel) # 手机号合法性判断,确保手机号正确 mail=input("\t请输入联系人邮箱:") mail =self.check_email(mail) #对邮件合法性的判断 weixin=input("\t请输入联系人微信:") card={'name':name,'adr':adr,'tel':tel,'E-mail':mail,'WC':weixin} txt = open('C:\\Users\\小张\\Desktop\\python\\通讯录', mode='a+', encoding='utf-8') txt.write(str(card)+'\n') txt.close() for i in range(0, 49): print("■", end="") time.sleep(0.02) print() print("\t\t\t\t添加成功!") def Del(self):#2.删除联系人 txt = open('C:\\Users\\小张\\Desktop\\python\\通讯录', mode='r+', encoding='utf-8') if len(txt.read())!=0: txt.seek(0, 0) t1 = txt.read() t2 = t1.split('\n') t2.remove(t2[len(t2) - 1]) tel = [] all=[] for i in t2: m = json.loads(i.replace("\'", "\"")) all.append([m]) tel.append(m['tel']) # print(all) t = input("\t请输入要删除联系人的手机号:") for i in range(0, 49): print("■", end="") time.sleep(0.02) print() if t in tel: i=tel.index(t) all.pop(i) txt = open('C:\\Users\\小张\\Desktop\\python\\通讯录', mode='w+', encoding='utf-8') for i in all: for n in i: # print(n) txt.write(str(n)+'\n') # txt = open('C:\\Users\\小张\\Desktop\\python\\通讯录', mode='w+', encoding='utf-8') # txt.write(txt1) print("\t\t\t\t删除成功!") else: print("\t\t\t\t无此联系人信息!") else: print("\t\t\t\t此通讯录为空!") txt.close() def Search(self):#3.查找联系人 txt = open('C:\\Users\\小张\\Desktop\\python\\通讯录', mode='r', encoding='utf-8') if len(txt.read())!=0: txt.seek(0,0) t1=txt.read() t2=t1.split('\n') t2.remove(t2[len(t2) - 1]) tel=[] all=[] for i in t2: m=json.loads(i.replace("\'","\"")) all.append(m) tel.append(m['tel']) t = input("\t请输入要查找联系人的手机号:") for i in range(0, 49): print("■", end="") time.sleep(0.02) print() if t in tel: for person in all: for key,val in person.items(): if t==val: for title,value in person.items(): print(title+":"+value+"\t",end="") print("") print("\t\t\t\t查找成功!") else: print("\t\t\t\t无此联系人信息,请先添加!") else: print("\t\t\t\t此通讯录为空!") txt.close() def Updata(self):#4.修改联系人信息 txt = open('C:\\Users\\小张\\Desktop\\python\\通讯录', mode='r+', encoding='utf-8') if len(txt.read()) != 0: txt.seek(0, 0) t1 = txt.read() t2 = t1.split('\n') t2.remove(t2[len(t2) - 1]) tel = [] all = [] for i in t2: m = json.loads(i.replace("\'", "\"")) all.append([m]) tel.append(m['tel']) # print(all) t = input("\t请输入要修改的联系人的姓名:") # name = input("\t请输入联系人名字:") adr = input("\t请输入联系人地址:") te = input("\t请输入联系人电话:") mail = input("\t请输入联系人邮箱:") qq = input("\t请输入联系人QQ:") for i in range(0, 49): print("■", end="") time.sleep(0.02) print() if t in tel: i = tel.index(t) all[i]=[{'name':t,'adr':adr,'tel':te,'E-mail':mail,'QQ':qq}] txt = open('C:\\Users\\小张\\Desktop\\python\\通讯录', mode='w+', encoding='utf-8') for i in all: for n in i: # print(n) txt.write(str(n) + '\n') # txt = open('C:\\Users\\小张\\Desktop\\python\\通讯录', mode='w+', encoding='utf-8') # txt.write(txt1) print("\t\t\t\t修改成功!") else: print("\t\t\t\t无此联系人信息!") else: print("\t\t\t\t此通讯录为空!") txt.close() def Print(self):#5.打印通讯录 print("*" * 49) txt = open('C:\\Users\\小张\\Desktop\\python\\通讯录', mode='r', encoding='utf-8') if len(txt.read())!=0: txt.seek(0,0) t1=txt.read() # print(t1) t2=t1.split('\n') # print(t2) t2.remove(t2[len(t2)-1]) # print(t2) a=[] for i in t2: m=json.loads(i.replace("\'","\"")) a.append(m) # print(a) for i in range(0, 49): print("■", end="") time.sleep(0.01) print() for person in a: # print(person) for key,val in person.items(): print(key+":"+val+"\t",end="") print("") else: print("\t\t\t\t此列表为空!") # for i in txt.readlines(): # print(i) txt.close() print("\t\t\t\t打印成功!") def main(self): print("\t\t\t\t正在启动程序,请稍后.....") for i in range(0,49): print("■",end="") time.sleep(0.05) print() print("\t\t\t\t程序启动成功!") while True: self.Show() n =input("请输入您的选项:") # 将1~5的选项作为键,功能函数名作为值写入字典中,起到多分支选择作用 select = { '1': self.Add, '2': self.Del, '3': self.Search, '4': self.Updata, '5': self.Print} if n in select: select[n]() # 通过选项访问功能函数名,以调用功能函数 else: print("输入选项错误,请重新输入!") if __name__=="__main__": tb=Tel() tb.main()
如果觉得还不错,请点个赞或打赏一块钱以资鼓励哦,我这绝对算是CSDN中的良心文档了!!!