python beginner user login implementation process of example explains
- 2020-06-19 10:50:04
- OfStack
Required to write login interface:
1. Enter your username and password
2. Welcome message will be displayed after successful authentication
3. User name input error, prompt the user does not exist, re-input (5 errors, prompt too many attempts, exit the program)
4. User name is correct, password is wrong, prompt password is wrong, re-enter (password is wrong 3 times, lock user name and prompt, exit the program)
readme
Application knowledge:
1. Operation of documents
Basic operation
f = open('lyrics','r',) # Open the file
first_line = f.readline()
print('first line:',first_line) # read 1 line
data = f.read()# Read all the rest , Don't use the file when it is large
print(data) # Print documents
f.close() # Close the file
The mode to open the file is as follows:
r, read-only mode (default).
w, write mode only. 【 unreadable; create if it does not exist; delete if it does exist; 】
a, append mode. [Readable; created if it does not exist; appended if it does exist;]
with statement, open multiple files at the same time, also can avoid open the file after forgetting to close, syntax:
with open('file1', 'r', encoding='utf-8') as f , \
open('file2', 'w', encoding='utf-8') as f_2 :
...
In this example, there are two files, one with username and password, and the other with blacklist. How to convert the file into python object after reading it, and then operate on the object, is the first difficulty I encountered!
And then I look it up, and I find the string split method, which breaks the string from the comma separator, and I get a list of substrings, so that I can find the blacklist and so on.
...
user_lis_bak = f.readline().split(',')
...
The setdefault dictionary setdefault() function is similar to the get() method in that if the key does not already exist in the dictionary, the key is added and the value is set to the default.
dict.setdefault(key, default=None)
Lists and dictionaries
List is one of the most commonly used data types in the future. Through list, data can be stored and modified in the most convenient way
names = ['xiaoli',"xiaoming",'yuanlu']
Access elements in a list with subscripts that count from 0
>>> names[0]
'xiaoli'
>>> names[1]
'xiaoming'
>>> names[-1]
'yuanlu'
>>> names[-2] # You can also do it backwards
'xiaoming'
A dictionary is a data type of ES57en-ES58en. Use a dictionary like the one we used in school. Use the strokes and letters to look up the details of the corresponding page.
info = {
'xiaoli': "123456",
'xiaoming': "111111",
'yuanlu': "888888",
}
Here key is the username,value is the password,11 corresponds, and key is the only one.
3. for loop and if... else
for i in range(10): print(i)
The above procedure, but encountered more than 5 cycles do not go, direct exit
for i in range(10):
if i>5:
continue # It's not going down , Go straight down 1 time loop
print(i)
User name and password file format:
xiaoli 123456
xiaoming 888888
wangpeng 111111
luyuan 112222
qiling 556666
haiming 223333
Blacklist file format:
xiaoli,xiaoming,wangpeng,
code
with open('user_lis', 'r', encoding='utf-8') as f_user_lis,\
open('user_lis_bak', 'r', encoding='utf-8') as f_user_lis_bak: # Open the file 'user_lis' and 'user_lis_bak'
user_lis_bak = f_user_lis_bak.read().split(',') # read f_user_lis_bak And turn into a list to assign to user_lis_bak
user_lis_dict = {} # define 1 An empty dictionary
for i in f_user_lis:
user_lis_dict.setdefault(i.split()[0], i.split()[1]) # read f_user_lis And put the value in the dictionary user_lis_dict In the
count = 0
for i in range(5): # Set the maximum number of errors to 5 time
name = input(' Please enter the user name >>')
password = input(' Please enter your password. >>')
if name in user_lis_bak: # The first 1 First, look for the blacklist
print(' The user name is locked , Please contact the administrator !!!')
continue # Go directly to the blacklist 1 loops , Re-enter the username and password
elif name in user_lis_dict: # The first 2 Step To find the user whitelist
if password == user_lis_dict.get(name): # Password is correct , Verification by exit
print(' Dear Member ', name, ' Welcome back ')
break
else: # Password mistake , Prompt error
if count == 2:
name_bak_write = open('user_lis_bak', 'a', encoding='utf-8')
name_bak_write.write(name+',')
name_bak_write.close() # 3 Secondary password error , Just put the username on the blacklist
print(' The user name is locked , Please contact the administrator !!!')
break
print(' Password mistake !!!')
count += 1 # The password is wrong 1 time ,count it +1
else:
if i == 4: # The user name wrong 5 time , Directly out of
print(' Too many attempts , goodbye !!!')
continue
print(' The username does not exist !!!') # User name error , Prompt error