利用字典的特性编写一个通讯录,记录姓名和联系(contact)电话
welcome = """ 欢迎进入通讯录程序 1:查找联系人资料 2:插入新的联系人 3:删除联系人 4:退出程序 """ print(welcome) addressBook = {} # with open('addressBook.txt', 'r+') as f: # content = f.read() # for eachline in content: # (name, phone) = eachline.split(':') # addressBook[name] = phone while True: # 循环用来多次操作直到退出程序 n = int(input('请输入相应的选项:')) if n == 1: name = input("请输入要查找的联系人:") if name in addressBook: print(name + ':' + addressBook[name]) else: print('查无此人!') elif n == 2: name = input("请输入新的联系人姓名:") if name in addressBook: print(name + '已经在通讯录里面') else: phone = input("请输入新的联系人的联系电话:") addressBook[name] = phone print(name + ':' + phone + '已添加') elif n == 3: name = input("请输入要删除的联系人:") if not(name in addressBook): print(name + '不在通讯录里面') else: del addressBook[name] print(name + ':' + '已删除') elif n == 4: print('再见!') break