Old boy homework day1 - user login

Keywords: Python

The first assignment I wrote after the old boy's training

Demand:

1. Users can log in by user name and password;

2. Lock the user after three incorrect password entries

3. Put the user name, password and locked user in the file

4. If you don't have the user prompt, you haven't registered

5. Login with the locked user will prompt that the user has been locked

 1 #! /usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 username = raw_input("username: ")
 5 password = raw_input("password: ")
 6 
 7 
 8 # Put user files in the dictionary to make it easier to match
 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_dict
17 
18 
19 # Check whether the input user is in the blacklist. If so, it will show that the user is already in the blacklist
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 # Reenter the wrong password. If it is more than three times, the password will be stopped
28 def input_again():
29     count = 0
30     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             break
36         else:
37             count += 1
38     else:
39         print("Sorry")
40     return count
41 
42 
43 # If the password is entered incorrectly three times, the user will be automatically added to the blacklist
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)

Problems still exist:

1. Multiple users cannot be added to the user file. Only one user can log in. When storing the user dictionary, because the key of the dictionary is unique, only the later user information can be saved

 

 

Posted by shanx24 on Thu, 02 Apr 2020 05:01:47 -0700