1. 程式人生 > >python 初學習 模擬用戶登錄

python 初學習 模擬用戶登錄

shel __name__ print 密碼 tao 連續 while lis 初學

#!/usr/bin/env python
#coding:utf-8
‘‘‘
2017年8月19日
  python 模擬用戶登錄,連續失敗次數超過3次永輝名被鎖定
userfile 文件保存字典 用戶名,和密碼
sorryname 文件保存字典 登錄過的的用戶名,登錄成功,值為0,登錄失敗,值+1
當值大於等於3是,賬號被鎖定,無法進行登錄
‘‘‘
import shelve
# 私有屬性,保存文件名
__userfile = ‘loginname.dat‘
__users = ‘users‘
__sorryname = ‘sorrylogin.dat‘
__sorry = ‘sorryuser‘
#登錄功能
def login(name,passwd):
userfile = shelve.open(__userfile)
users = userfile[__users]
sorryname = shelve.open(__sorryname)
sorry = sorryname[__sorry]
try:
if name in users:
#登錄成功
if passwd == users[name]:
sorry[name]=0
return ‘登錄成功‘
else: # 失敗
if name in sorry:
sorry[name]=sorry[name]+1
else:
sorry[name]=1
return ‘密碼錯誤‘
else:
return ‘沒有此用戶,請註冊‘
finally:
sorryname[__sorry]=sorry
sorryname.close()
if __name__ == "__main__":    
#userfile = shelve.open(‘loginname.dat‘)
#users = {‘fuzhentao‘:‘123456‘,‘zhangsan‘:‘456789‘,‘lisi‘:‘147852‘}
#userfile[__users]=users
#userfile.close()
‘‘‘sorryname = shelve.open(__sorryname)
sorryuser={}
sorryname[__sorry]=sorryuser
sorryname.close()‘‘‘
while True:
sorryname = shelve.open(__sorryname)
sorry = sorryname[__sorry]
name = input(‘請輸入用戶名:‘)
passwd = input(‘請輸入密碼:‘)
if name in sorry and sorry[name]>=3:
print("賬號被鎖定")
else:
print(login(name,passwd))

python 初學習 模擬用戶登錄