您好,登錄后才能下訂單哦!
如下所示:
#!/usr/bin/env python #coding: utf8 import getpass db = {} def newUser(): username = raw_input('username: ') if username in db: #添加打印顏色 print "\033[32;1m%s already exists![0m" % username else: #屏幕不顯示密碼,調(diào)用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)uit input 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]()#這種方法相當(dāng)于shell和c里面的case,很實(shí)用 if __name__ == '__main__': showMenu()
以上這篇用Python寫一段用戶登錄的程序代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。