如下所示:
#!/usr/bin/env python#coding: utf8import getpassdb = {}def newUser(): username = raw_input('username: ') if username in db: #添加打印顏色 print "/033[32;1m%s already exists![0m" % username else: #屏幕不顯示密碼,調用getpass.getpass() password = getpass.getpass() db[username] = password #字典k-v賦值def oldUser(): username = raw_input('username: ') password = getpass.getpass() if username in db: if db.get(username) == password:#判斷輸入的用戶名和密碼是否和字典的k-v匹配 print '/033[32;1mlogin successful!/033[0m' else: print '/033[32;1mpassword not match username/033[0m' else: print '/033[32;1musername does not exist/033[0m'CMDs = {'n':newUser,'o':oldUser}def showMenu(): prompt = """(N)ew user(O)ld user(Q)uitinput your choice: """ while True: try:#捕獲ctrl+c ctrl+d的異常 choice = raw_input(prompt).strip().lower()[0] except (KeyboardInterrupt, EOFError): choice = 'q' if choice not in 'noq': continue if choice == 'q': break CMDs[choice]()#這種方法相當于shell和c里面的case,很實用if __name__ == '__main__': showMenu()
以上這篇用Python寫一段用戶登錄的程序代碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。
新聞熱點
疑難解答