溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

用Python寫一段用戶登錄的程序代碼

發(fā)布時(shí)間:2020-09-25 09:36:27 來源:腳本之家 閱讀:174 作者:huangyingleo 欄目:開發(fā)技術(shù)

如下所示:

#!/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寫一段用戶登錄的程序代碼

以上這篇用Python寫一段用戶登錄的程序代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(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)容。

AI