新使用者注冊,老使用者登陸,使用者名和密碼是否正确
import getpass # 登入的時候密碼不顯示
userdb = {} # 定義使用者字典
def register():
# 注冊.首先判斷使用者名在清單中是否存在
username = input('輸入使用者名:')
if username in userdb:
print('\033[31;1m使用者已存在\033[0m')
else:
password = input('請輸入密碼:')
userdb[username] = password
print('\033[32;1m建立成功\033[0m')
def login():
# 登入.首先判斷是否存在該使用者,再判斷密碼是否正确
username = input('輸入使用者名:')
if username not in userdb:
print('\033[31;1m使用者名不存在\033[0m')
else:
password = getpass.getpass('輸入密碼')
if userdb.get(username) == password:
print('\033[32;1m登入成功\033[0m')
else:
print('\033[31;1m登入失敗\033[0m')
def show_menu():
# 顯示在螢幕上的資訊,定義使用者的三種選擇
cmds = {'0': register, '1': login}
prompt = """(0) 注冊
(1) 登入
(2) 退出
請選擇(0/1/2): """
while True:
choice = input(prompt).strip()
if choice not in ['0', '1', '2']:
print('無效輸入,請重試')
continue
if choice == '2':
print('Bye-bye')
break
cmds[choice]() #判斷0和1的簡寫
if __name__ == '__main__':
show_menu()