使用python实现名片功能

card_main.py

import card_tool
while True:
    card_tool.show_menu()
    action_str = input("请选择希望执行的操作:")
    print("您选择的输入是 %s" %action_str)
    if action_str in ["1","2","3"]:
        if action_str == "1":
            card_tool.new_card()
        elif action_str == "2":
            card_tool.show_all()
        elif action_str == "3":
            card_tool.search_card()
    elif action_str == "0":
        print("欢迎再次使用[名片管理系统]")
        break
    else:
        print("您选择的不正确超出范围,请重新输入")
        

card_tool.py

#记录所有的名片字典
card_list = []
def show_menu():
    print("*" * 50)
    print("欢迎使用【名片管理系统】")
    print("")
    print("1.新增名片")
    print("2.显示全部")
    print("3.搜索名片")
    print("")
    print("0.推出系统")
    print("*" * 50)

def new_card():
    print("-" * 50)
    print("新增名片")
    name_str = input("请输入姓名:")
    phone = input("请输入电话:")
    qq = input("请输入QQ:")
    email = input("请输入邮箱:")

    card_dict = {"name": name_str,
                 "phone": phone,
                 "qq": qq,
                 "email": email}
    card_list.append(card_dict)
    print(card_list)
    print("添加%s成功" % name_str)

def show_all():
    print("-" * 100)
    print("显示所有名片:")
    #判断是否存在名片记录
    if len(card_list) == 0:
        print("当前没有任何名片记录,请添加名片")
        return
    for name in ["姓名","电话","qq","邮箱"]:
        print(name, end="\t\t")
    print("")
    print("-" * 100)
    for card_dict in card_list:
        print("%s\t\t%s\t\t%s\t\t%s\t\t" % (card_dict["name"],
                                            card_dict["phone"],
                                            card_dict["qq"],
                                            card_dict["email"]))

def search_card():
    print("-" * 50)
    print("搜索名片")
    #提示用户需要搜索的姓名
    find_name = input("请输入需要搜索的姓名:")
    for card_dict in card_list:
        if card_dict["name"] == find_name:
            print("找到了,haha")
            print("姓名\t\t电话\t\tqq\t\t邮箱")
            print("=" * 50)
            print("%s\t\t%s\t\t%s\t\t%s" % (card_dict["name"],
                                            card_dict["phone"],
                                            card_dict["qq"],
                                            card_dict["email"]))
            # 删除修改
            deal_card(card_dict)
            break
    else:
        print("抱歉没有找到 %s" % find_name)

def deal_card(find_dict):
    """
    jjjjjjj
    :param find_dict:
    """
    print(find_dict)
    action_str = input("请选择需要修改的操作:"
    "1修改 2删除 0 返回上级菜单")
    if action_str == "1":
        find_dict["name"] = input_card_info(find_dict["name"],"姓名")
        find_dict["phone"] = input_card_info(find_dict["phone"],"电话")
        find_dict["qq"] = input_card_info(find_dict["qq"],"qq")
        find_dict["email"] = input_card_info(find_dict["email"],"邮箱")
        print("修改成功")
    elif action_str == "2":
        card_list.remove(find_dict)
        print("删除成功")

def input_card_info(dict_value,tip_message):
    #1.提示用户输入内容
    result_str = input(tip_message)
    #2.针对用户的输入惊醒判断,如果输入了内容,直接返回结果
    if len(result_str) > 0:
        return result_str
    #3.如果没有输入,返回字典原有的值
    else:
        return dict_value

你可能感兴趣的:(python)