Python实现简易名片管理系统:增删改查完整代码解析
本文详细介绍使用Python实现一个简易名片管理系统的完整过程。系统涵盖添加、删除、修改、查询及显示所有名片五大核心功能。通过解析代码逻辑,重点讲解如何利用列表存储数据、isnumeric()方法验证手机号以及递归处理异常输入,帮助初学者掌握Python基础数据类型与流程控制的实际应用。
前言
此前虽然浏览过Python教程,能看懂基础代码,但独立编写程序仍感吃力。为解决这一痛点并应对求职需求,我重新研读教材并大量练习编码。在看到名片管理系统这一经典题目后,我决定借此机会复习Python中常用的数据类型,并花费时间完成了该系统的编程实现。
Python实现名片管理系统
该系统旨在实现以下核心功能:
*****************\n\n名片管理系统
1.添加名片
2.删除名片
3.修改名片
4.查询名片
5.退出系统
0.显示所有名片
*****************
添加名片
编程思路:首先创建一个临时列表变量templist,利用templist.append()方法依次添加姓名、手机号和地址等信息。完成信息收集后,将该临时列表追加到主列表mainList中,从而完成名片的添加操作。
def increMem(aList):
tempList = []
tempName = input("输入新建名片名字:")
tempList.append(tempName)
while True:
tempPhone = input("输入新建联系人手机号:")
if tempPhone.isnumeric(): break
else: print("输入有误,重新输入")
tempList.append(tempPhone)
tempAddr = input("输入新建联系人地址:")
tempList.append(tempAddr)
print("输入新建联系人信息:")
showList(tempList)
aList.append(tempList)
注意:
手机号必须为数字格式。可以通过list.isnumeric()方法判断输入是否为纯数字字符串,若返回False则说明输入无效。
删除名片
编程思想:首先判断列表是否为空,若为空则提示并返回。接着定位待删除联系人的索引值,最后通过del()函数执行删除操作。
def delMem(aList):
i = 0
if len(aList) == 0 :
print("没有联系人,请先添加联系人!")
return
tempName = input("输入要删除的联系人:")
for mumList in aList:
if tempName != mumList[0] :
i += 1
continue
else:
showList(aList[i])
while True:
tempIn = input("是否删除此联系人: Y(是)\t N(否) :")
if tempIn =="Y" or tempIn == "y":
del(aList[i])
print("删除成功!")
return
elif tempIn == "N" or tempIn == "n":
print("重新输入联系人!")
delMem(aList)
return
else:
print("输入有误,重新输入!")
if i == len(aList):
print("输入的联系热不存在,请重新输入!")
delMem(aList)
注意:
若删除的联系人不存在,需进行特殊处理。遍历mainList,每个元素均为list结构。若当前元素numList[0](姓名)不等于要删除的联系人姓名,则索引i自增1并继续循环。若遍历完所有元素均未找到(即i等于列表长度),则提示联系人不存在,要求重新输入。
修改名片
修改名片的逻辑是先定位目标,再进行修改。
def modMem(aList):
i = 0
if len(aList) == 0 :
print("没有联系人,请先添加联系人!")
return
tempList = input("输入需要修改的联系人:")
for numList in aList:
if tempList != numList[0] :
i += 1
continue
else:
tempInf = input("输入修改的信息:")
if tempInf.isnumeric():
numList[1] = tempInf
else:
numList[2] = tempInf
if i == len(aList):
print("输入有误,重新输入!")
modMem(aList)
注意:
利用isnumeric()方法判断输入内容:若全为数字,则视为修改电话号码;否则视为修改地址。
查找名片
查找逻辑为先定位目标,再输出信息。需特别注意分析当联系人不存在时的处理情况。
def LocaMem(aList):
i = 0
if len(aList) == 0 :
print("没有联系人,请先添加联系人!")
return
tempList = input("输入需要查找的联系人:")
for numList in aList:
if tempList != numList[0] :
i += 1
continue
else:
showList(numList)
if i == len(aList):
print("输入有误,重新输入!")
modMem(aList)
完整的程序块
def men():
print("\t*****************")
print("\t 名片管理系统\n")
print("\t 1.添加名片\n")
print("\t 2.删除名片\n")
print("\t 3.修改名片\n")
print("\t 4.查询名片\n")
print("\t 5.退出系统\n")
print("\t 0.显示所有名片\n")
print("\t*****************")
def increMem(aList):
tempList = []
tempName = input("输入新建名片名字:")
tempList.append(tempName)
while True:
tempPhone = input("输入新建联系人手机号:")
if tempPhone.isnumeric(): break
else: print("输入有误,重新输入")
tempList.append(tempPhone)
tempAddr = input("输入新建联系人地址:")
tempList.append(tempAddr)
print("输入新建联系人信息:")
showList(tempList)
aList.append(tempList)
def showList(aList):
print("名字: %s"%aList[0],\
"电话:%s"%aList[1], \
"地址:%s"%aList[2],"\n")
def showMem(aList):
if len(aList) == 0:
print("没有联系人!")
for mumList in aList:
print("名字: %s"%mumList[0],\
"电话:%s"%mumList[1], \
"地址:%s"%mumList[2],"\n")
def delMem(aList):
i = 0
if len(aList) == 0 :
print("没有联系人,请先添加联系人!")
return
tempName = input("输入要删除的联系人:")
for mumList in aList:
if tempName != mumList[0] :
i += 1
continue
else:
showList(aList[i])
while True:
tempIn = input("是否删除此联系人: Y(是)\t N(否) :")
if tempIn =="Y" or tempIn == "y":
del(aList[i])
print("删除成功!")
return
elif tempIn == "N" or tempIn == "n":
print("重新输入联系人!")
delMem(aList)
return
else:
print("输入有误,重新输入!")
if i == len(aList):
print("输入的联系热不存在,请重新输入!")
delMem(aList)
def modMem(aList):
i = 0
if len(aList) == 0 :
print("没有联系人,请先添加联系人!")
return
tempList = input("输入需要修改的联系人:")
for numList in aList:
if tempList != numList[0] :
i += 1
continue
else:
tempInf = input("输入修改的信息:")
if tempInf.isnumeric():
numList[1] = tempInf
else:
numList[2] = tempInf
if i == len(aList):
print("输入有误,重新输入!")
modMem(aList)
def LocaMem(aList):
i = 0
if len(aList) == 0 :
print("没有联系人,请先添加联系人!")
return
tempList = input("输入需要查找的联系人:")
for numList in aList:
if tempList != numList[0] :
i += 1
continue
else:
showList(numList)
if i == len(aList):
print("输入有误,重新输入!")
modMem(aList)
if __name__ == "__main__":
mainList = []
men()
while True:
index = input("输入任务编号:")
if not index.isnumeric():
print("请输入索引编号(1-4):")
continue
index = int(index)
#遍历名片
if index == 0:
showMem(mainList)
#增加名片
if index == 1:
increMem(mainList)
if index == 2:
delMem(mainList)
if index == 3:
modMem(mainList)
if index == 4:
LocaMem(mainList)
if index == 5:
print("退出系统!")
break
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













