用戶管理
如果輸入 delete, 則讓用戶輸入” 用戶名” 格式字符串, 根據用戶名查找 dict 中數據, 若
存在數據則將該數據移除, 若用戶數據不存在, 則提示不存在;
如果輸入 update, 則讓用戶輸入” 用戶名:年齡:聯繫方式” 格式字符串, 並使用:分隔用戶
數據, 根據用戶名查找 dcit 中數據, 若存在數據則將改數據更新數據, 若用戶數據不存在,
則提示不存在;
如果用戶輸入 find, 則讓用戶輸入” 用戶名” 格式字符串, 根據用戶名查找 dict 中數據包
含輸入字符串的用戶信息, 並打印;
如果用戶輸入 list, 則打印所有用戶信息;
打印用戶第一個行數據爲用戶信息描述, 從第二行開始爲用戶數據;
如果用戶輸入 exit, 則打印退出程序, 並退出 ;
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# Date:2018-5-13
# Author:AreLIN
UserDict = {"AreLIN": {"age": "25", "phone": "110"},
"Boy": {"age": "18", "phone": "120"},
"Teacher": {"age": "18", "phone": "119"},
"Studet": {"age": "20", "phone": "991"}
}
UserChoseInfo = '''
************************
Please input your chose:
1. delete
2. update
3. find
4. list
5. exit
************************
'''
UserChoseList = {"1": "delete", "2": "update", "3": "find", "4": "list", "5": "exit"}
Flag = True
while Flag:
UserChose = input(UserChoseInfo+"\n--->")
if UserChose in UserChoseList.keys():
if int(UserChose) == 1:
print(UserDict)
DeleteName = input("從上面的字典選擇你要刪除的用戶名\n--->")
if DeleteName in UserDict:
del UserDict[DeleteName]
print(UserDict)
else:
print("你選擇的用戶不存在")
Flag = False
elif int(UserChose) == 2:
UpdateUser = input("用戶名:年齡:聯繫方式\n--->")
UpdateList = UpdateUser.split(":")
if UpdateList[0] in UserDict.keys():
UpdateChose = input("你所添加的用戶已經存在,如果想更改請輸入Yes,如果不添加請輸入任意字符:\n--->")
if UpdateChose == "Yes":
UserDict.update({UpdateList[0]: {"age": UpdateList[1], "Phone": UpdateList[2]}})
else:
print("選擇不更改原用戶")
pass
else:
UserDict.update({UpdateList[0]: {"age": UpdateList[1], "Phone": UpdateList[2]}})
print(UserDict)
elif int(UserChose) == 3:
FindName = input("請輸入查找的用戶名\n--->")
if FindName in UserDict.keys():
print({FindName: UserDict[FindName]})
elif int(UserChose) == 4:
print(UserDict)
else:
print("Good bye - * —")
break
else:
print("You\'r chose is wrong,Please chose agin: %s" % UserChoseInfo)