1.使用模块
sys
2.代码
import sys
#菜单
def menu():
print("1、添加联系人")
print("2、查找联系人")
print("3、删除联系人")
print("4、查看联系人")
print("5、退出")
a = input("请输入1到5,选择相应功能:")
return int(a)
peoples = []
#添加联系人
def add():
people = {}
name = input("输入名字:")
age = input("输入年龄:")
tel = input("输入手机号:")
wchat = input("输入微信号:")
people["name"] = name
people["age"] = age
people["tel"] = tel
people["wchat"] = wchat
peoples.append(people)
#查找联系人
def find():
name = input("输入要查找的名字:")
f = 0 #标记作用
for i in peoples:
if(i["name"] == name):
# print(i)
f = 1
print("年龄:",i["age"])
print("手机:",i["tel"])
print("微信:",i["wchat"])
if f == 0:
print("联系人不存在")
#删除联系人
def delt():
name = input("输入要删除的名字:")
f = 0 #标记作用
for i in peoples:
if(i["name"] == name):
peoples.remove(i)
f = 1
print("删除成功")
if f == 0:
print("联系人不存在")
#查看所有联系人
def view():
if len(peoples)==0:
print("没有联系人")
else:
print("姓名 年龄 手机号 微信号")
for i in peoples:
print(i['name'],' ',i['age'],' ',i['tel'],' ',i['wchat'])
while True:
m = menu()
if(m == 1):
add()
elif(m == 2):
find()
elif(m == 3):
delt()
elif(m == 4):
view()
elif(m == 5):
sys.exit()
3.模块sys
sys