[Python的学习之路] 练习 简单的名片管理系统

list1 = []

def show_card():

print("****************************************")
print("*           名片管理系统V1.0            *")
print("*             1. 新建名片               *")
print("*             2. 全部名片               *")
print("*             3. 查询名片               *")
print("*             4. 修改名片               *")
print("*             0. 退出系统               *")
print("****************************************")

def add_card():

print("*功能: 新建名片            输入0返回上一层")
print("****************************************")
while True:
    name = input("*请输入姓名: ")
    if name == "0":
        break
    while len(name) <= 6 or len(name) >= 20:
        print("*姓名长度不符合6-20位以内")
        name = input("*请输入姓名: ")
    age = input("*请输入年龄: ")
    if age == "0":
        break
    phone = input("*请输入电话号码: ")
    if phone == "0":
        break
    while len(phone) != 11:
        print("*电话号码长度不符合11位")
        phone = input("*请输入电话号码: ")
    QQ = input("*请输入QQ号码: ")
    if QQ == "0":
        break
    email = input("*请输入电子邮箱: ")
    if email == "0":
        break
    company = input("*请输入所属公司: ")
    if company == "0":
        break
    while len(phone) != 11:
        print("*电话号码长度不符合11位")
        phone = input("*请输入电话号码: ")
    dict1 = {"姓名": name, "年龄": age, "电话号码": phone,\
             "QQ": QQ, "邮箱": email, "所属公司": company}
    for key in dict1:
        while len(dict1[key]) == 0:
            dict1[key] = input("*%s不能为空,请重新输入:" % key)
    list1.append(dict1)
    break
print("*添加成功")
print("****************************************")
print("****************************************")
print("*           名片管理系统V1.0            *")
print("*             1. 继续添加               *")
print("*             2. 返回上一级             *")
print("****************************************")
while True:
    a = input("*请输入您要操作的序号: ")
    if int(a) == 1:
        add_card()
        break
    elif int(a) == 2:
        break
    else:
        print("*输入错误")

def all_card():

print("*功能: 查看全部名片")
for dict1 in list1:
    print("****************************************")
    for key in dict1:
        print_card1(dict1, key)
    print("****************************************")

def search_card():

print("*功能: 搜索单个名片")
print("****************************************")
a = input("*请输入您要搜索的信息: ")
print("****************************************")
for dict1 in list1:
    for key in dict1:
        if a in dict1[key]:
            print_card(dict1)
            print("****************************************")
else:
    print("没有名片信息")
    print("****************************************")
print("****************************************")
print("*           名片管理系统V1.0            *")
print("*             1. 继续查询               *")
print("*             2. 修改名片               *")
print("*             3. 删除名片               *")
print("*             4. 返回上一级             *")
print("****************************************")
while True:
    a = input("*请输入您要操作的序号: ")
    if int(a) == 1:
        search_card()
        break
    elif int(a) == 2:
        alter_card()
        break
    elif int(a) == 3:
        del_card()
        break
    elif int(a) == 4:
        break
    else:
        print("*输入错误")

def alter_card():

dict2 = {}
print("*功能: 修改名片")
print("****************************************")
a = input("*请输入你要修改的信息的姓名: ")
print("****************************************")
for dict1 in list1:
    for name in dict1.keys():
        if dict1[name] == a:
            print_card(dict1)
            dict2.update(dict1)
            list1.remove(dict1)
print("****************************************")
b = input("*请输入你要修改的项: ")
c = input("*请输入你要修改的值: ")
print("****************************************")
while True:
    if b == "姓名":
        key = "name"
        break
    elif b == "年龄":
        key = "age"
        break
    elif b == "电话":
        key = "phone"
        break
    elif b == "QQ":
        key = "QQ"
        break
    elif b == "邮箱":
        key = "email"
        break
    elif b == "公司":
        key = "company"
        break
    else:
        b = input("*没有该项,请重新输入: ")
dict2[key] = c
list1.append(dict2)
print_card(dict2)
print("*修改完成...")
print("****************************************")
print("*           名片管理系统V1.0            *")
print("*             1. 继续修改               *")
print("*             2. 返回上一级             *")
print("****************************************")
while True:
    a = input("*请输入您要操作的序号: ")
    if int(a) == 1:
        alter_card()
        break
    elif int(a) == 2:
        break
    else:
        print("*输入错误")

def del_card():

print("*功能: 删除名片")
print("****************************************")
a = input("*请输入你要删除的名片的姓名: ")
print("****************************************")
for dict1 in list1:
    for name in dict1.keys():
        if dict1[name] == a:
            list1.remove(dict1)
print("*删除成功...")
print("****************************************")
print("****************************************")
print("*           名片管理系统V1.0            *")
print("*             1. 继续删除               *")
print("*             2. 返回上一级             *")
print("****************************************")
while True:
    a = input("*请输入您要操作的序号: ")
    if int(a) == 1:
        del_card()
        break
    elif int(a) == 2:
        break
    else:
        print("*输入错误")

def print_card(dict1):

for key in dict1:
    if key == "name":
        print("\t  姓名:\t%s" % dict1[key])
    elif key == "age":
        print("\t  年龄:\t%s岁" % dict1[key])
    elif key == "phone":
        print("\t  电话:\t%s" % dict1[key])
    elif key == "QQ":
        print("\t   QQ: \t%s" % dict1[key])
    elif key == "email":
        print("\t  邮箱:\t%s" % dict1[key])
    elif key == "company":
        print("\t  公司:\t%s" % dict1[key])

def print_card1(dict1, key):

if key == "name":
    print("\t  姓名:\t%s" % dict1[key])
elif key == "age":
    print("\t  年龄:\t%s岁" % dict1[key])
elif key == "phone":
    print("\t  电话:\t%s" % dict1[key])
elif key == "QQ":
    print("\t   QQ: \t%s" % dict1[key])
elif key == "email":
    print("\t  邮箱:\t%s" % dict1[key])
elif key == "company":
    print("\t  公司:\t%s" % dict1[key])

while True:

main.show_card()

a = input("请输入您要操作的序号: ")
if int(a) == 1:
    main.add_card()
elif int(a) == 2:
    main.all_card()
elif int(a) == 3:
    main.search_card()
elif int(a) == 4:
    main.alter_card()
elif int(a) == 0:
    print("正在退出......")
    print("退出成功")
    break
else:
    print("输入错误")
    continue
    原文作者:Sniper
    原文地址: https://segmentfault.com/a/1190000014855918
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞