Week1:作业1
#coding=utf-8 #Version:python3.6.0 #Tools:Pycharm 2017.3.2 __date__ = '2018/9/8 23:37' __author__ = 'Steven' ''' 作业二:编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 '''
user_lib = {} with open('account_list.txt', 'r') as account_list: #comb = account_list.read() for line in account_list: comb = line.split() user_lib[comb[0]] = [int(comb[1]),int(comb[2])] #print(user_lib) count = 0 TryTimeLimit = 4 for count in range(TryTimeLimit): username = input("Please input your username:") password = int(input("Please input your password:")) #password = getpass.getpass("Please input your password") if username in user_lib: if user_lib[username][1] >= 3: print("Permanently locked account, please contact the admin... Username:", username) break if password == user_lib[username][0]: print("Welcome user {name} logon".format(name = username)) user_lib[username][1] = 0 #登录成功时清空错误次数累计 break else: user_lib[username][1] += 1 print("Invalid username or password ! Please check and try later..") if user_lib[username][1] >= 3: print("Account locked According to security policy !!! Username:", username) else: print("Invalid username or password ! Please check and try later..") print("you have only {guess_chance} times left".format(guess_chance = TryTimeLimit - 1 - count)) else: print("You have tried too many times..") try: with open('account_list.txt', 'w') as account_list2: for keys in user_lib: # temp_lines[line_index] = [str(keys), str(user_lib[keys][0]), str(user_lib[keys][1])] temp_lines = [str(keys), ' ', str(user_lib[keys][0]),' ', str(user_lib[keys][1]), '\n'] account_list2.writelines(temp_lines) account_list2.close() except: print("Fator Error: File writing failed! ")
文件内容(原始):
Eric 123456 0
Mike 147258 0
Kevin 234567 0
Emma 345678 0