創建一個名片管理系統,實現增、刪、改、查、四項功能
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('請輸入正確選項!')