registered:
#1、先把文件内容读出来 user_info = {} fw = open('users','a+',encoding='utf-8') fw.seek(0) for line in fw: line = line.strip() if line!='': username,password,error_count = line.split(',') user_info[username] = {'password':password,'error_count':int(error_count)} for i in range(3): username = input('username:').strip().lower() password = input('password:').strip() cpassword = input('cpassword:').strip() if username=='' or password=='' or cpassword=='': print('账号?密码、确认密码不能为空') elif len(username)<6 orlen (username)> 12 is or len (password) <. 6 or len (password)> 12 is : Print ( ' length username / password minimum 6, maximum 12 ' ) elif password =! cPassword: Print ( ' two input inconsistent password ' ) elif username in USER_INFO: Print ( ' user name already exists! ' ) the else : USER_STR is = ' % S,% S, 0 \ n- ' % (username, password) fw.write (USER_STR is) fw.flush () # user_info[username] = {'password': password, 'error_count': 0} print('注册成功!') # break fw.close()
log in
# 2, log 2PY # . 1, account number, password is not blank # 2, to verify whether the account the presence, absence prompted to # 3, a maximum of three times the input # 4, account insensitive # 5, account number, and password to greater than or equal to 6 and less than or equal to 12 # 6, when the wrong password each time you log in, you add back this account of a number of failures, # 7, and if that fails, then the number is greater than three times, suggesting that the account has been locked. Import datetime USER_INFO = {} FW = Open ( ' Users ' , ' A + ' , encoding = ' UTF-. 8 ' ) fw.seek (0) for Line in FW: Line = line.strip () IF line!='': username,password,error_count = line.split(',') user_info[username] = {'password':password,'error_count':int(error_count)} for i in range(3): username = input('username:').strip().lower() password = input('password:').strip() if username=='' or password=='': Print ( ' account password can not be empty? ' ) Elif <. 6 len (username) or len (username)> 12 is or len (password) <. 6 or len (password)> 12 is : Print ( ' Minimum length of the ID / password 6, maximum 12 ' ) elif username not in user_info: Print ( ' ! user name does not exist ' ) elif user_info [username] [ ' ERROR_COUNT ' ]> 3 : Print ( ' enter the wrong password too many times!') elif user_info.get(username).get('password') !=password: print('密码错误!') user_info[username]['error_count']+=1 else: print('登录成功!%s'%datetime.datetime.now()) break fw.seek(0) fw.truncate() for username,user_dict in user_info.items(): password= user_dict.get('password') error_count= user_dict.get('error_count') user_str = '%s,%s,%s\n'%(username,password,error_count) fw.write(user_str) fw.flush() fw.close()