python程序—名片管理系统

创建一个名片管理系统,实现增、删、改、查、四项功能

listcard = []
while True:
    print('**********欢迎来到名片管理系统**********')
    print('      1.查看名片')
    print('      2.创建名片')
    print('      3.修改名片')
    print('      4.删除名片')
    print('      5.退出名片')
    print('*' * 30)
    choose = input('请选择:').strip()
    # 查看
    if choose == '1':
        if listcard:
            i = 0
            while i < len(listcard):
                print('%s--姓名:%s  年龄:%s  电话:%s' % (i+1, listcard[i]['name'], listcard[i]['age'], listcard[i]['phone']))
                i += 1
        else:
            print('没有信息!')
    # 增加
    elif choose == '2':
        new_name = input('name:').strip()
        new_age = input('age:').strip()
        new_phone = input('phone:').strip()
        if new_name and new_age and new_phone:
            info = {}.fromkeys(('name', 'age', 'phone'), None)
            info['name'] = new_name
            info['age'] = new_age
            info['phone'] = new_phone
            listcard.extend([info])
            print('名片创建成功!')
        else:
            print('请输入相应的信息!')

    # 删除
    elif choose == '3':
        if listcard:
            i = 0
            while i < len(listcard):
                print('%s--姓名:%s|年龄:%s|phone:%s' % (i+1, listcard[i]['name'], listcard[i]['age'], listcard[i]['phone']))
                i += 1
        res = input('请输入要删除的名片序号:')
        listcard.remove(listcard[int(res)-1])
        print('删除成功!')

    # 修改
    elif choose == '4':
        i = 0
        while i < len(listcard):
            print(
                '%s--姓名:%s|年龄:%s|phone:%s' % (i, listcard[i]['name'], listcard[i]['age'], listcard[i]['phone']))
            i += 1
        res = input('请输入要修改的名片序号:')
        print('请输入修改的内容:')
        edit_name = input('姓名(回车不修改):').strip()
        edit_age = input('年纪(回车不修改):').strip()
        edit_phone = input('电话(回车不修改):').strip()
        if edit_name:
            listcard[int(res)-1]['name'] = edit_name
        if edit_age:
            listcard[int(res)-1]['age'] = edit_age
        if edit_phone:
            listcard[int(res)-1]['phone'] = edit_phone
        print('修改成功!')

    #退出
    elif choose == '5':
        print('谢谢使用!')
        break
    else:
        print('请输入正确选项!')

可以根据名字查询,删除,修改的名片管理系统

lt=[]
while True:
    print('**********欢迎来到名片管理系统**********')
    print('  a:新建  b:修改 c:删除 d:查询  e:退出')
    print('*' * 40)
    ks = input('请输入要进行的操作:')

    # 增
    if ks == 'a':
        info = {}.fromkeys(('name', 'age', 'phone'), None)
        k = len(lt)
        lt.extend([info])
        lt[k]['name'] = input('name:')
        lt[k]['age'] = input('age:')
        lt[k]['phone'] = input('phone:')
        print('名片已经添加!')
        for i, j in lt[k].items():
            print(i, ':', j)

    # 改
    elif ks == 'b':
        key=input('要修改的名字:')
        i=0
        while i < len(lt):
            if key in lt[i].values():
                lt[i]['name'] = input('name:')
                lt[i]['age'] = input('age:')
                lt[i]['phone'] = input('phone:')
                print('名片已经修改!')
            else:
                print('没有该名片!')
            i += 1


    # 删
    elif ks == 'c':
        key=input('要删除的名字:')
        i=0
        while i < len(lt):
            if key in lt[i].values():
                lt[i].clear()
                lt.remove(lt[i])
                print('名片已经删除!')
            else:
                print('没有该名片!')
            i += 1


    # 查
    elif ks == 'd':
        key=input('要查询的名字:')
        i=0
        while i < len(lt):
            if key in lt[i].values():
                for k, v in lt[i].items():
                    print(k, ':', v)
                print('')
            else:
                print('没有该名片!')
            i += 1

    # 退出
    elif ks == 'e':
        print('谢谢使用!')
        break
    else:
        print('请输入正确选项!')

猜你喜欢

转载自www.cnblogs.com/leeeel/p/10758187.html