不用模块和函数
# 需求:建立一个名片系统,功能有新建名片,查找全部名片,搜索名片,退出系统
# 菜单栏
# 1.新建名片
# 2.查找名片
# 3.搜索名片
# 搜索名片如果找到了之后,可以对数据进行修改和删除
stu_list = [{'name': '张三', 'tel': '8852654102', 'QQ': '1456429876', 'email': '[email protected]'},
{'name': '李四', 'tel': '7449526502', 'QQ': '8506987456', 'email': '[email protected]'},
{'name': '王五', 'tel': '5557952412', 'QQ': '3569874568', 'email': '[email protected]'},
{'name': '赵六', 'tel': '1595265402', 'QQ': '1456987456', 'email': '[email protected]'},
{'name': '一百', 'tel': '86549526512', 'QQ': '2588987456', 'email': '[email protected]'},
] # 定义一个列表储存字典,
while True:
"""欢迎菜单"""
print("*" * 60)
print()
print("1.新建名片")
print("2.显示全部")
print("3.搜索名片")
print()
print()
print("0.退出系统")
print("*" * 60)
user = input("请输入你要执行的操作:")
# 新建名片
if user == "1":
# 定义用户的姓名,电话,QQ,邮箱
name = input("请输入你的姓名:")
tel = input("请输入你的电话:")
QQ = input("请输入你的QQ号码:")
email = input("请输入你的邮箱")
# 定义字典接收
stu_word = {"name": name, 'tel': tel, 'QQ': QQ, 'email': email}
# 将字典数据添加到列表中
stu_list.append(stu_word)
print("新建名片成功,姓名:%s\t\t\t电话:%s\t\t\tQQ号:%s\t\t\t邮箱:%s" % (name, tel, QQ, email))
# 2.查询名片
# stu_list = [] 储存字典的数据
elif user == "2":
print("姓名\t\t电话\t\t\t\t\tQQ\t\t\t\t\t\t邮箱")
for i in stu_list: # for循环遍历列表中包裹的字典
print("%s\t\t%s\t\t\t%s\t\t%s" % (i['name'], i['tel'], i['QQ'], i['email']))
# 3.搜索名片
# 搜索名片如果找到了之后,可以对数据进行修改和删除
# stu_list = [] 储存字典的数据
elif user == "3":
name_word = input("请输入你要搜索的姓名:")
for word in stu_list: # for循环遍历列表中包裹的字典
if word['name'] == name_word: # 使用if判断进行比较
print("姓名\t\t\t电话\t\t\t\t\t\tQQ\t\t\t\t\t邮箱")
print("%s\t\t\t%s\t\t\t%s\t\t\t%s" % (word['name'], word['tel'], word['QQ'], word['email']))
##############
# 修改搜索字典和删除字典
user_2 = input("请输入你要执行的操作:1-编辑数据,2-删除数据,0-返回上一级")
if user_2 == "1":
user_2_name = input("请输入新姓名:")
user_2_tel = input("请输入新电话号码:")
user_2_QQ = input("请输入新的QQ:")
user_2_email = input("请输入新的邮箱:")
# 字典赋值
word['name'] = user_2_name
word['tel'] = user_2_tel
word['QQ'] = user_2_QQ
word['email'] = user_2_email
print(word)
##########
# 删除搜索的字典
elif user_2 == "2":
stu_list.remove(word)
print("已删除该字典")
##########
else:
print("你的输入有误,已退回初始界面")
break
else:
print("没有找到你搜索的值,请重新输入")
# 退出循环
elif user == "0":
print("你已经成退出循环")
break
else:
print("对不起,你的输入有误,请重新输入")
使用模块和函数
import card # 调用模块(py文件)
while True:
card.show_menu() # 调用模块中的函数
user = input("请输入你要执行的操作:")
if user == "1": # 新建菜单
card.new_card()
elif user == "2": # 查询名片
card.show_all()
elif user == "3": # 搜索名片
card.search_card()
elif user == "0":
print("你已经退出系统")
break
else:
print("您的输入有误,请重新输入")
模块代码
def search_card():
“”“搜索名片”""
name_word = input(“请输入你要搜索的姓名:”)
for word in stu_list: # for循环遍历列表中包裹的字典
if word[‘name’] == name_word: # 使用if判断进行比较
print(“姓名\t\t\t电话\t\t\t\t\t\tQQ\t\t\t\t\t邮箱”)
print("%s\t\t\t%s\t\t\t%s\t\t\t%s" % (word[‘name’], word[‘tel’], word[‘QQ’], word[‘email’]))
##############
# 修改搜索字典和删除字典
user_2 = input("请输入你要执行的操作:1-编辑数据,2-删除数据,0-返回上一级")
if user_2 == "1":
user_2_name = input("请输入新姓名:")
user_2_tel = input("请输入新电话号码:")
user_2_QQ = input("请输入新的QQ:")
user_2_email = input("请输入新的邮箱:")
word['name'] = user_2_name
word['name'] = user_2_tel
word['name'] = user_2_QQ
word['name'] = user_2_email
print(word)
##########
# 删除搜索的字典
elif user_2 == "2":
stu_list.remove(word)
print("已删除该字典")
##########
else:
print("你的输入有误,已退回初始界面")
break
else:
print("没有找到搜索的姓名:%s" % name)