用python编写程序完成通讯录查询功能。1 从文件phonenumtxt中读入电话通讯录。每行格式如下 :
读取文件内容并存入字典中
with open("phonenum.txt", "r", encoding="utf-8") as f: lines = f.readlines() phonenum_dict = {} for line in lines: name, phone = line.strip().split() phonenum_dict[name] = phone
查询功能
name = input("请输入姓名:") if name in phonenum_dict: print("{}:{}".format(name, phonenum_dict[name])) else: print("数据不存在")
原文地址: https://www.cveoy.top/t/topic/g0hK 著作权归作者所有。请勿转载和采集!