一、思路
1、定義名片操作選項
2、把增加的名片資訊存儲到字典中
3、所有名片資訊存儲到清單
4、對于誤操作給出提示
二、用到的知識點
1、類的定義,用來設定控制台輸出顔色
三、效果
四、代碼
"""
* @Author: xiaofang
* @software: PyCharm
* @Description: 名片管理系統
"""
class BColors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def cardHead():
print(BColors.HEADER)
print('=======歡迎進入名片管理系統=======')
print('1.檢視名片')
print('2.添加名片')
print('3.修改名片')
print('4.删除名片')
print('5.退出系統')
print(BColors.ENDC)
l = [] # 使用清單,進行資料的增删改查
while True:
cardHead()
choose = input('請選擇: ') # input 輸出都是字元串
print(BColors.OKBLUE)
if choose == '1':
i = 0
if len(l) == 0:
print('暫無名片')
else:
while i < len(l):
print('%s->姓名:%s | 年齡:%s | 身高:%s' % (i, l[i]['name'], l[i]['age'], l[i]['high']))
i += 1
elif choose == '2':
name = input('name: ').strip()
age = input('age: ').strip()
high = input('high: ').strip()
info = {'name': name, 'age': age, 'high': high}
l.append(info)
print('添加成功')
elif choose == '3':
revise = input('請選擇要修改的名片的ID: ')
if int(revise) >= len(l):
print('該ID不存在')
else:
name1 = input('name: ')
age1 = input('age ')
high1 = input('high: ')
if name1:
l[int(revise)]['name'] = name1
if age1:
l[int(revise)]['age'] = age1
if high1:
l[int(revise)]['high'] = high1
print('修改成功')
elif choose == '4':
del1 = input('請選擇要删除的名片: ')
if int(del1) >= 0 and int(del1) < len(l):
l.remove(l[int(del1)])
print('删除成功')
else:
print('該ID不存在')
elif choose == '5':
print('退出成功,歡迎使用本簡易名片系統')
break
else:
print('輸出錯誤,請重新輸入')
print(BColors.ENDC)