1. 程式人生 > >python32 用來儲存名片函式方式

python32 用來儲存名片函式方式

# 用來儲存名片

card_infors = []

 

 

def print_menu():

    """完成列印功能選單"""

    print("=" * 50)

    print("   名片管理系統 V0.01")

    print(" 1. 新增一個新的名片")

    print(" 2. 刪除一個名片")

    print(" 3. 修改一個名片")

    print(" 4. 查詢一個名片")

    print(" 5. 顯示所有的名片")

    print(" 6. 退出系統")

    print("=" * 50)

 

 

def add_new_card_infor():

    """完成新增一個新的名片"""

    new_name = input("請輸入新的名字:")

    new_qq = input("請輸入新的QQ:")

    new_weixin = input("請輸入新的微信:")

    new_addr = input("請輸入新的住址:")

 

    # 定義一個新的字典,用來儲存一個新的名片

    new_infor = {}

    new_infor['name'] = new_name

    new_infor['qq'] = new_qq

    new_infor['weixin'] = new_weixin

    new_infor['addr'] = new_addr

 

    # 將一個字典,新增到列表中

    global card_infors

    card_infors.append(new_infor)

 

    # print(card_infors)# for test

 

 

def find_card_infor():

    """用來查詢一個名片"""

 

    global card_infors

 

    find_name = input("請輸入要查詢的姓名:")

    find_flag = 0  # 預設表示沒有找到

    for temp in card_infors:

        if find_name == temp["name"]:

            print("%s\t%s\t%s\t%s" % (temp['name'], temp['qq'], temp['weixin'], temp['addr']))

            find_flag = 1  # 表示找到了

            break

 

    # 判斷是否找到了

    if find_flag == 0:

        print("查無此人....")

 

 

def show_all_infor():

    """顯示所有的名片資訊"""

 

    global card_infors

 

    print("姓名\tQQ\t微信\t住址")

    for temp in card_infors:

        print("%s\t%s\t%s\t%s" % (temp['name'], temp['qq'], temp['weixin'], temp['addr']))

 

 

def main():

    """完成對整個程式的控制"""

    # 1. 列印功能提示

    print_menu()

 

    while True:

 

        # 2. 獲取使用者的輸入

        num = int(input("請輸入操作序號:"))

 

        # 3. 根據使用者的資料執行相應的功能

        if num == 1:

            add_new_card_infor()

        elif num == 2:

            pass

        elif num == 3:

            pass

        elif num == 4:

            find_card_infor()

        elif num == 5:

            show_all_infor()

        elif num == 6:

            break

        else:

            print("輸入有誤,請重新輸入")

 

        print("")

 

 

# 呼叫主函式

main()

 

如有疑問,請發郵件:[email protected]


github:https://github.com/wangrui0/