本文實例講述了Python實現(xiàn)的用戶登錄系統(tǒng)功能。分享給大家供大家參考,具體如下:
有N,E,Q三個選擇,若選擇Q或者中斷,則系統(tǒng)退出。若其他選項,則持續(xù)讓用戶選擇。
#!/usr/bin/env pythondb = {}def newuser(): prompt = 'login desired: ' while 1: name = raw_input(prompt) if db.has_key(name): prompt = 'name taken, try another: ' continue else: break pwd = raw_input('passwd: ') db[name] = pwddef olduser(): name = raw_input('login: ') pwd = raw_input('passwd: ') passwd = db.get(name) if passwd == pwd: pass else: print 'login incorrect' return print 'welcome back', namedef showmenu():##主函數(shù)名 prompt = """(N)ew User Login(E)xisting User Login(Q)uitEnter choice: """ done = 0 ##控制循環(huán)的控制器 while not done: chosen = 0 ##控制循環(huán)的控制器 while not chosen: try: choice = raw_input(prompt)[0] except (EOFError, KeyboardInterrupt): choice = 'q' print '/nYou picked: [%s]' % choice if choice not in 'neq':##非neq繼續(xù)小循環(huán) print 'invalid menu option, try again' else: chosen = 1 ##跳出小循環(huán) if choice == 'q': done = 1 ##跳出大循環(huán) if choice == 'n': newuser() if choice == 'e': olduser()if __name__ == '__main__': showmenu()運行結(jié)果:

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學運算技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答