这个管理系统主要包括三个部分,1、新建名片(包括姓名、电话、QQ、邮箱);2、显示全部名片;3、查询名片(键入要查询的信息,若查到则可以选择修改/删除操作,反之则不)
将下面两个.py文件(do_cards.py(第一段代码),hello_users.py(第二段代码))放在同一目录下,直接运行hello_users.py:
class Execute:
def __init__(self, data):
self.data = data
def add_new_cards(self, num):
"""
添加名片
:return:添加后的数据
"""
index = 1
keys = ['姓名', '电话', 'QQ', '邮箱']
while index <= num:
print('输入第%d个数据:' % index)
new_dict = dict.fromkeys(keys)
for key in new_dict:
new_dict[key] = input(key + '为:')
self.data.append(new_dict)
index += 1
return self.data
def query_card(self,search_name):
"""
查到指定名片后,用户可以选择修改或者删除名片
:return:
"""
find = []
for one_dict in self.data:
for key, value in one_dict.items():
if value == search_name:
find.append(one_dict)
if find:
return find
else:
return '未找到'
def delete_cards(self,search_name):
"""
删除该名片
:return: 删除后的数据,删除的个数
"""
item = self.query_card(search_name)
for i in self.data:
if i in item:
self.data.remove(i)
return self.data, len(item)
def update_cards(self,search_name):
data = self.delete_cards(search_name)
new_data = self.add_new_cards(data[1])
return new_data
if __name__ == '__main__':
news = [{'姓名': '小明', '电话': '110', 'QQ': '12345', '邮箱': '12345@qq.com'},
{'姓名': '小花', '电话': '120', 'QQ': '54321', '邮箱': '54321@qq.com'}]
cards = Execute(news)
print(cards.delete_cards('110'))
import do_cards
keys = ['姓名', '电话', 'QQ', '邮箱']
user_list = []
with_cards = do_cards.Execute(user_list)
def hello_users():
"""
名片管理系统的主界面
:return:
"""
print('*' * 50)
print('欢迎使用【名片管理系统】')
print('1、新建名片')
print('2、显示全部')
print('3、查询名片')
print('')
print('0、退出系统')
print('*' * 50)
def add_cards():
"""
记录用户的姓名、电话、QQ、邮件
:return:卡片列表
"""
num = input('请您输入需要添加用户的个数:')
while not num.isdecimal() or len(num) < 0:
print('输入有误,请重新输入')
num = input('请您输入需要添加用户的个数:')
with_cards.add_new_cards(int(num))
return user_list
hello_users()
cards = []
choice = input('请选择操作功能:')
while choice != '0':
if (choice not in ['0', '1', '2', '3']) or (len(choice) < 1):
while (choice not in ['0', '1', '2', '3']) or (len(choice) < 1):
print('您输入的内容有误,请重新输入!')
choice = input('请选择操作功能:')
else:
print('您选择的操作是:%d' % int(choice))
# 添加数据
if int(choice) == 1:
cards = add_cards()
print('添加成功!')
# 显示所有数据
elif int(choice) == 2:
if cards:
for i, j in enumerate(cards):
if i == 0:
for key, value in j.items():
print('{0:<20}'.format(key), end='\t')
print('')
for key, value in j.items():
print('{0:<20}'.format(value), end='\t')
print('')
else:
print('暂无数据,请添加数据!')
# 查询数据(修改/删除)
elif int(choice) == 3:
search_name = input('请输入你要查找的姓名/电话/QQ/邮箱:')
message = with_cards.query_card(search_name)
if message == '未找到':
print('未找到内容!')
else:
edit_choice = input('请选择操作(0:不操作,1:修改名片,2、删除名片):')
while len(edit_choice) < 1 or edit_choice not in ['0', '1', '2']:
print('输入有误,请重新输入')
edit_choice = input('请输入0:不操作,1:修改名片,2、删除名片')
if edit_choice == '0':
print('暂无操作!')
elif edit_choice == '1':
with_cards.update_cards(search_name)
print('修改成功!')
elif edit_choice == '2':
with_cards.delete_cards(search_name)
print('删除成功!')
print('')
hello_users()
choice = input('请选择操作功能:')
else:
print('退出系统!')
欢迎指正!
作者:cxyuanaa