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