⽤Python写⼀段⽤户登录的程序代码
如下所⽰:
#!/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:
#屏幕不显⽰密码,调⽤pass()
password = pass()
db[username] = password #字典k-v赋值
def oldUser():
username = raw_input('username: ')
password = pass()
if username in db:
(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: """
python新手代码示例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写⼀段⽤户登录的程序代码就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论