1. 程式人生 > >模擬用戶登陸註冊

模擬用戶登陸註冊

ces scrip import cmd 輸入 gis choices 實現 password

編寫login.py腳本,實現以下目標:

  1. 支持新用戶註冊,新用戶名和密碼註冊到字典中
  2. 支持老用戶登陸,用戶名和密碼正確提示登陸成功
  3. 主程序通過循環詢問進行何種操作,根據用戶的選擇,執行註冊或是登陸操作
    #!/usr/local/bin/python3
    # -*- conding : utf-8 -*-
    """
    @Time         :  19-3-16 上午10:45
    @Author       :  xq
    @Software     :  PyCharm
    @File         :  userlogin.py
    @Description   :
    """
    import
    getpass userdb = {} def register(): username = input(item to username: ).strip() if username not in userdb: password = input(item to userpasswd: ).strip() userdb[username] = password #key不在字典就添加 else: print(%s already exitsts!!% username)
    def login(): username = input(username: ) password = getpass.getpass(password: ) # if username in userdb and userdb[username] == password: # if userdb.get(username) == password: if userdb[username] == password: #判斷鍵值等不等 print(Login successful) else
    : print(login failed) def show_menu(): prompt = """(0)register (1)login (2)quit please choices(0/1/2) : """ while True: cmd = {0: register,1: login} choice = input(prompt).strip() # 去除用戶輸入的兩端的空白字符 if choice not in [0,1,2]: print("Invalid input,please try again") continue if choice == 2: break cmd[choice]() if __name__ == "__main__": show_menu()

模擬用戶登陸註冊