Python learning _1_ for simple login judgment

Source: Internet
Author: User

"""
Job: Let the user enter a user name and password, verify three times, three unsuccessful, lock the last user.
"""


Def login1 ():
"" The method of implementing login Verification yourself:
According to what has been learned to think of the method. The disadvantage is that the user name password can only store one.
"""

User_info_file = open (' User_info.txt ', ' R ') # User name and password store file
User_info_list = User_info_file.readlines () # Correct user name and password storage queue
Username = User_info_list[0].strip () # Correct user name
Password = user_info_list[1] # correct password
User_info_file.close ()

For I in range (3):

Username_input = input (' Please output User name: ')
Password_input = input (' Please enter password: ')
Login_count = 2-i # Determine how many times you can enter.
Lock_user_file = open (' Lock_user.txt ', ' R ') # blacklist file

For line in Lock_user_file.readlines ():
If Line.strip () = = Username_input:
Print (' User cannot log in, please contact admin to unlock. ‘)
Return False

Else
If username = = Username_input and Password = = Password_input:
Print (' Login successful ')
Return True
elif i = = 2:
Lock_user_file_add_user = open (' Lock_user.txt ', ' a ')
Lock_user_file_add_user.write (' \ n ')
Lock_user_file_add_user.write (Username_input)
Lock_user_file_add_user.close ()
Print (' login exceeded ' limit, user is locked. ‘)
Return False
Else
Print (' Username or password error, you can also try to login ', Login_count, ' times. ‘)

Lock_user_file.close ()


Def login2 ():
"" "Comprehensive video textbook modified method, optimized place:

1. Use the WITH statement to open the file and avoid the close file.
2, you can split a row of different content, each row can save a user information.
"""

User_info_file = ' user_info.txt ' # User name and password store file
Lock_user_file = ' lock_user.txt ' # blacklist file

For I in range (3):

Username_input = input (' Please output User name: ')
Login_count = 2-i # Determine how many times you can enter.

With open (Lock_user_file, ' R ') as Lock_user: # To determine if the user is blacklisted, the blacklist jumps out, and the function feedback is false.
For line in Lock_user.readlines ():
If Line.strip () = = Username_input:
Print (' User cannot log in, please contact admin to unlock. ‘)
Return False

Password_input = input (' Please enter password: ')

With open (User_info_file, ' R ') as User_info: # Determines if the user is logged on successfully, the login succeeds, and the function feedback is true.
For line in User_info.readlines ():
Username, password = Line.strip (). Split ()
If username = = Username_input and Password = = Password_input:
Print (' Login successful ')
Return True

if i = = 2: # If user input error three times, then blacklist, and jump out, function feedback false.
Lock_user_file_add_user = open (Lock_user_file, ' a ')
Lock_user_file_add_user.write (' \ n ' +username_input)
Lock_user_file_add_user.close ()
Print (' login exceeded ' limit, user is locked. ‘)
Return False
Else
Print (' Username or password error, you can also try to login ', Login_count, ' times. ‘)

Python learning _1_ for simple login judgment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.