實現要求:
例如:password = {'admin':'123321','user1':'123456'}
1.設計一個登錄程序,不同的用戶名和對應密碼存在一個字典里面,輸入正確的用戶和密碼去登錄
2.首先輸入用戶名,如果用戶名不存在或為空,則一直提示輸入正確的用戶名
3.當用戶名正確時,提示去輸入密碼,如果密碼跟用戶名不對應,提示密碼錯誤請重新輸入
4.如果密碼輸入錯誤超過3次,中斷程序運行
5.當輸入密碼錯誤時,提示還有幾次機會
6.用戶名和密碼都輸入成功的時候,提示登錄成功
實現代碼如下:
loginMessage = { 'username1': '1234', 'username2': '1234', 'username3': '1234', 'username4': '1234', 'username5': '1234' } count = 0 # 密碼輸入次數 while True: username = input('請輸入用戶名:') if username in loginMessage.keys(): while count < 3: pwd = input('請輸入密碼:') if pwd == loginMessage[username]: print("登錄成功!") break else: count+=1 print("密碼錯誤,請重新輸入") print("還有{0}次機會修改密碼".format(3 - count)) break elif username not in loginMessage.keys() or username =='': print("用戶名錯誤,請重新輸入")