Python implements username and password verification

The examples in this article share the specific code of python to achieve user name and password verification for your reference. The specific content is as follows

Need to implement function

Enter the username and password;
The welcome message will be displayed after successful authentication;
Locked after three consecutive mistakes;

readme:

Please see the code comments for details, I'm lazy
1、 Enter username and password
2、 Determine whether the username is in the locked file
3、 Determine whether the username and password are correct

Code

 # Enter username and password
 # A welcome message will be displayed after successful authentication
 # Locked after three consecutive incorrect entries
count =0while True:
 # Enter username and password
 username =input("Username:")
 password =input("Password:")
 # Open the username and password file
 f =open('login_true.txt')
 # Open locked username file
 f4 =open('login_lock.txt')
 # Determine whether the input user name exists in the locked file
 for line in f4:
 user_name2 = line.strip()if username == user_name2:print("username is locked!")breakelse:
 # Determine whether the username and password are correct
 for line in f:
  str = line.strip()
  str2 =','
  user_name = str[:str.index(str2)]
  pass_word = str[str.index(str2)+1:]
  # print(user_name, pass_word)
  # The username and password are correct, print the welcome message and clear the counter out of the loop
  if username == user_name and password == pass_word:print("welcome "+ username)
  count =0break
  # The user name is correct, the password is wrong, print the prompt message, the counter is incremented by 1, and judge whether the counter reaches 3
  elif username == user_name and password != pass_word:print("wrong password!")
  count +=1print(count)
  # The counter reaches 3 times, and the user name is written into the locked file
  if count ==3:
   f2 =open('login_lock.txt',"a+")
   f2.write(username+'\n')
   f2.close()
   count =0breakelse:print("please check out your username!")
  count =0
 f.close()

note

Create two new files in the same directory of the py file:
login_true.txt ———store the username and password in advance

login_lock.txt———-used to store the locked user name

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python implements username and password verification
Python implements password strength verification
Python implements verification code recognition
Python implements string and number splicing
Python implements singly linked lists and dictionaries
Python and Go
Python introspection and reflection
Python implements Super Mario
Python implements tic-tac-toe game
Python implements tic-tac-toe game
[python] python2 and python3 under ubuntu
Python implements man-machine gobang
Python implements Tetris game
Python implements image stitching
Python implements minesweeper game
Python implements scanning tools
Python deconstruction and packaging
Python3 configuration and entry.md
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python implements guessing game
Python automated operation and maintenance 2
Python introduction and environment installation
Python implements simple tank battle
Python know crawler and anti crawler
Python implements udp chat window
Python implements WeChat airplane game
centos7 install python3 and ipython
Python implements word guessing game
Python implements a guessing game
Python implements parking management system
Python implements digital bomb game
Python implements TCP file transfer
Python numpy implements rolling case
Centos 6.10 reinstall python and yum
OpenCV Python implements puzzle games
Python implements simple tic-tac-toe game
Python open read and write
Python implements car management system
CentOS7 install python3 and pip3
Python implements code block folding
Python implements panoramic image stitching
Python data structure and algorithm
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
Python implements mean-shift clustering algorithm
CentOS 6.9 compile and install python
Python implements gradient descent method
Python implements text version minesweeper
CentOS 6 compile and install python 3
Generators and iterators in Python
Python implements image stitching function
Python implements the brick-and-mortar game