本文实例讲述了Python实现的用户登录系统功能。分享给大家供大家参考,具体如下:
有N,E,Q三个选择,若选择Q或者中断,则系统退出。若其他选项,则持续让用户选择。
#!/usr/bin/env python
db = {}
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] = pwd
def 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', name
def showmenu():##主函数名
prompt = """
(N)ew User Login
(E)xisting User Login
(Q)uit
Enter choice: """
done = 0 ##控制循环的控制器
while not done:
chosen = 0 ##控制循环的控制器
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继续小循环
print 'invalid menu option, try again'
else:
chosen = 1 ##跳出小循环
if choice == 'q': done = 1 ##跳出大循环
if choice == 'n': newuser()
if choice == 'e': olduser()
if __name__ == '__main__':
showmenu()
运行结果:
希望本文所述对大家Python程序设计有所帮助。