跟着老男孩培训写的第一次作业
需求:
1、用户可以通过用户名和密码登陆;
2、密码输入错误三次后即锁定用户
3、把用户名、密码、锁定后的用户放在文件里
4、如果没有该用户提示还没有注册
5、使用锁定的用户登陆会提示此用户已经被锁定了
1 #! /usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 username = raw_input("username: ") 5 password = raw_input("password: ") 6 7 8 # 把用户文件放到字典里面,更加容易匹配 9 def read_config():10 with open(r'E:\coding\oldboy\practice_day1\user_info', 'r') as f:11 user_dict={}12 for line in f.readlines():13 user_info = line.strip().split('=')14 user_dict[user_info[0]] = user_info[1]15 # user_dict['password'] = user_info[3]16 return user_dict17 18 19 # 检查输入的用户是否在黑名单里,如果在,显示已经在黑名单里面20 def chack_locked():21 with open(r'E:\coding\oldboy\practice_day1\lock_info', 'r') as f_lock_info:22 if username in f_lock_info.read():23 print("You are locked")24 exit(0)25 26 27 # 密码输入错误后重新输入,超过三次则会停止输入密码28 def input_again():29 count = 030 while count < 2:31 print("It is wrong password")32 password = raw_input("password again: ")33 if password in read_config()['password']:34 print('congratulition')35 break36 else:37 count += 138 else:39 print("Sorry")40 return count41 42 43 # 如果输入密码错误三次,则自动把用户添加到黑名单里面44 def locked(count):45 if count == 2:46 with open('E:\coding\oldboy\practice_day1\lock_info', 'a') as f_lock:47 f_lock.write(username)48 49 50 if __name__ == '__main__':51 chack_locked()52 user_dict = read_config()53 if username == user_dict['username'] and password == user_dict['password']:54 print("Welcome to login")55 elif username != user_dict['username']:56 print("No user, please sign up")57 elif username == user_dict['username'] and password != user_dict['password']:58 count = input_again()59 locked(count)
仍然存在的问题:
1、用户文件里无法添加多个用户,只能有一个用户登陆,把用户存放字典时,因为字典的键唯一,所以只能保存后面的用户信息