Python_自学程序2_模拟通讯录_待完善

来源:互联网 发布:黑客帝国3矩阵革命 mp4 编辑:程序博客网 时间:2024/06/04 19:42
# -*- coding: cp936 -*-
contacts = {
        '1': {
            'Name': 'ruju.xie',
            'Tel': '57189',
            'Email': 'ruju.xie@163.com',
            'Team': 'ST1'},


        '2': {
            'Name': 'mingna.qi',
            'Tel': '52611',
            'Email': 'mingna.qi@tcl.com',
            'Team': 'ST1'},


        '3': {
            'Name': 'birong.she',
            'Tel': '57480',
            'Email': 'birong.she@tcl.com',
            'Team': 'ST1'},


        '4': {
            'Name': 'simin.xu',
            'Tel': '52643',
            'Email': 'simin.xu@tcl.com',
            'Team': 'ST1'},


        '5': {
            'Name': 'xiaoqin.duan',
            'Tel':'57184',
            'Email': 'xiaoqin.duan@163.com',
            'Team': 'ST1'},


        '6': {
            'Name': 'zhiqiang.tong',
            'Tel': '52652',
            'Email': 'zhiqiang.tong@tcl.com',
            'Team': 'VPM'},


        '7': {
            'Name': 'shitao.qiu',
            'Tel': '57103',
            'Email': 'shitao.qiu@tcl.com',
            'Team': 'SPM'},
        '8': {
            'Name': 'qiong.li',
            'Tel': '57043',
            'Email': 'qiong.li@tcl.com',
            'Team': 'VPM'},


        '9': {
            'Name': 'shilin.chen',
            'Tel': '52676',
            'Email': 'shilin.chen@tcl.com',
            'Team': 'SWD2'},


        '10': {
            'Name': 'ke.li',
            'Tel': '57032',
            'Email': 'ke.li@tcl.com',
            'Team': 'SWD2'},


        '11': {
            'Name': 'zexiang.li',
            'Tel': '736',
            'Email': 'zexiang.li@tcl.com',
            'Team': 'NB'}}




def SHOW_ALL(c):
    print "\n"
    print "---------------------------------------------------------"
    print "---------------All Contacts List as below----------------"
    print "---------------------------------------------------------"
    if len(c) == 0:
        print "\nPhonebook is empty. Do you want to add a new one\n"
        print "1: Yes"
        print "2: No"
        new = raw_input()
        if new == "1":
            ADD(c)
    #创建表头
    for c_keys, c_values in c.items():
        for p_keys, p_values in c_values.items():
            print "%-15s" % p_keys,
            continue
        print "\n"
        break
    #输出联系人信息
    for c_keys, c_values in c.items():
        for p_keys, p_values in c_values.items():
            print "%-15s" % p_values,
        print "\n"




def ADD(A):
    print "\nPlease input contact info:"
    new_c = {}
    new_c.fromkeys(['name', 'tel', 'email', 'team'])
    new_c['name'] = raw_input("Name: ")
    new_c['tel'] = raw_input("Tel: ")
    new_c['email'] = raw_input("Email: ")
    new_c['team'] = raw_input("Team: ")
    number = len(A)
    A[number+1] = new_c
    print """\n---------------------Add successfully--------------------\n"""
    SHOW_ALL(A)




def SEARCH(c):
    result = -1
    info = raw_input("Please input the info to search:")
    for c_keys, c_values in c.items():
        for p_keys, p_values in c_values.items():
            if c[c_keys][p_keys].find(info) != -1:
                result = 1
    if result == 1:
        #绘制表头
        for c_keys, c_values in c.items():
            for p_keys, p_values in c_values.items():
                print "%-15s" % p_keys,
                continue
            print "\n"
            break
        #逐个输出查找到的联系人
        for c_keys, c_values in c.items():
            for p_keys, p_values in c_values.items():
                if c[c_keys][p_keys].find(info) != -1:
                    person = c[c_keys].values()
                    for i in range(len(person)):
                        print "%-15s" % person[i],
                    print "\n"
                    break
                else:
                    continue
    if result == -1:
        print "\n------------------------No Result------------------------\n"
        ADD(c)




def DELETE(D):
    info = raw_input("Please input the delete Keywords: ")
    for c_keys, c_values in D.items():
        for p_keys, p_values in c_values.items():
            if D[c_keys][p_keys].find(info) != -1:
                delete_person = c_keys
                D.pop(delete_person)
                break
    print "\n-------------------Delete successfully-------------------\n"
    SHOW_ALL(D)




while True:
    print "-"*57
    print "1: Show all"
    print "2: Add"
    print "3: Search"
    print "4: Delete"
    print "-" * 57
    request = raw_input("Please Choose Menu: ")


    if request == '1':
        SHOW_ALL(contacts)
    elif request == '2':
        ADD(contacts)
    elif request == '3':
        SEARCH(contacts)
    elif request == '4':
        DELETE(contacts)
    elif request != 1 or request != 2 or request != 3 or request != 4:
        print "Wrong info, try again!!!"
原创粉丝点击