python learning - realize user password login, input error three times and lock

Posted by patrick99e99 on Sat, 04 Apr 2020 07:08:43 +0200

Job requirements:

  1. Enter user name and password
  2. Show welcome message after authentication
  3. Lock after three wrong inputs

Implementation ideas:

  1. Determine whether the user is in the blacklist. If the user is in the blacklist, lock the user
  2. Judge whether the user exists. If not, prompt that the user does not exist
  3. If the user exists, judge whether the login password is correct. If it is correct, welcome the user to log in successfully and provide the user with three password input opportunities. If it is wrong, lock the account

Method 1:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
count = 0    #Counter
username = "aaa"  #Login user name
userpassword = "asd"   #Login password

#Read blacklist users
f = open("aaa.txt","r")
file_list = f.readlines()
f.close()
lock= []
name = input("Login user name:")

#Judge whether the user is in the blacklist
for i in file_list:
    line = i.strip("\n")
    lock.append(line)
if name in lock:
    print("Your account is locked. Please contact the administrator.")
else:
#If the user is not in the blacklist, judge whether the user exists
    if name == username:
    #If the password is entered incorrectly three times in a row, lock the account
        while count <3:
            password = input("Login password:")
            if name == username and password == userpassword:
                print("Welcome%s!"%name)
                break
            else:
                print("Account and password do not match")
                count +=1
        else:
            print("Sorry, your account has been locked for three consecutive wrong passwords. Please contact the administrator.")
            f = open("aaa.txt","w+")
            li = ['%s'%username]
            f.writelines(li)
            f.close()
    else:
        print("The user name does not exist. Please enter the correct user name.")

Mode two:

 1 #Create two new files, account.txt and account_lock.txt,stay account Input in aaa 123
 2 #among account.txt Read only, account_lock.txt Read and write
 3 import sys,os
 4 count = 0
 5 name_list = []
 6 while count < 3:
 7     name = input("Please enter the user name:")
 8     lock_file = open("account_lock.txt","r+")   #Read blacklist,"r+": Open a file for reading and writing,"r": Open file read-only
 9     lock_list = lock_file.readlines()
10     #.readlines(): Read the whole file at one time and analyze the content into a list of lines, and.read()equally
11     #.readline(): Read one row at a time, use when memory is not enough.readline()
12     for lock_line in lock_list:
13         lock_line = lock_line.strip('\n')
14         if name == lock_line:
15             #If the user name entered is in the blacklist, the program exits abnormally
16             sys.exit("user%s It has been locked. Please contact the administrator to unlock it." %name)
17     #Read the correct user name and password
18     user_file = open("account.txt","r")
19     user_list = user_file.readlines()
20     for user_line in user_list:
21         (user,password) = user_line.strip('\n').split()
22         name_list.append(user_line)    #.append(): Use to add a new object at the end of the list
23         print("---------",name_list)
24         #User name is correct, password input count
25         if name == user:
26             i = 0
27             while i<3:
28                 passwd = input("Please input a password:")
29                 if passwd == password:
30                     print("Welcome%s Sign in"%name)
31                     sys.exit(0)
32                 else:
33                     if i<2:
34                         print("user%s Wrong password, please re-enter, and%d Opportunity."%(name,2-i))
35                 i += 1
36             else:
37                 lock_file.write(name + '\n')
38                 sys.exit("user%s Enter the wrong password three times, the user will be locked and exit, please contact the administrator to unlock."%name)
39         else:
40             pass
41     else:
42         #User name input error count
43         if count < 2:
44             print("user%s No, please re-enter, and%d Second chance"%(name,2-count))
45         count += 1
46 else:
47     sys.exit("user%s No, exit"%name)
48 #Close open files
49 lock_file.close()
50 user_file.close()

Topics: Python