Python learning-Implement User Password Logon, three wrong locks, python User Password

Source: Internet
Author: User

Python learning-Implement User Password Logon, three wrong locks, python User Password

Job Requirements:

Implementation ideas:

Method 1:

#! /Usr/bin/env python3 #-*-coding: UTF-8-*-count = 0 # counter username = "aaa" # logon username userpassword = "asd" # logon password # Read the blacklist user f = open ("aaa.txt ", "r") file_list = f. readlines () f. close () lock = [] name = input ("Login Username:") # determine whether the user is blacklisted for I in file_list: line = I. strip ("\ n") lock. append (line) if name in lock: print ("your account is locked. Contact the administrator. ") Else: # if the user is not in the blacklist, determine whether the user exists if name = username: # if the password is wrong 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 attempts, contact the administrator. ") F = open (" aaa.txt "," w + ") li = ['% s' % username] f. writelines (li) f. close () else: print ("the user name does not exist. Enter the correct user name. ")

Method 2:

1. create two new files, account.txtand account_lock.txt. in the account, enter aaa 123 2. Then, account.txtformat is read only. account_lock.txt adopts the read/write Format 3 import sys, OS 4 count = 0 5 name_list = [] 6 while count <3: 7 name = input ("Enter the User name:") 8 lock_file = open ("account_lock.txt", "r +") # Read the blacklist, "r + ": open a file for reading and writing. "r": open the file in read-only mode 9 lock_list = lock_file.readlines () 10 #. readlines (): reads the entire file at a time, analyzes the content into a list of rows, and. same as read () 11 #. readline (): reads a row at a time and is used when the memory is insufficient. readline () 12 for lock_line in lock_list: 13 lock_line = lock_line.strip ('\ n') 14 if name = lock_line: 15 # if the input user name is in the blacklist, the program unexpectedly exits 16 sys. exit ("User % s has been locked. Please contact the Administrator to unlock it. "% name) 17 # Read the correct username 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 (): Used to add a new object 23 print ("---------", name_list) 24 at the end of the list # the user name is correct. The password input count is 25 if name = user: 26 I = 027 while I <:28 passwd = input ("Enter password:") 29 if passwd = password: 30 print ("Welcome % s login" % name) 31 sys. exit (0) 32 else: 33 if I <print ("User % s Password error, please enter again, there is a % d opportunity. "% (name, 2-i) 35 I ++ = 136 else: 37 lock_file.write (name + '\ n') 38 sys. exit ("User % s enters the wrong password three times. The user will be locked and exited. Please contact the Administrator to unlock the password. "% name) 39 else: 40 pass41 else: 42 # username input error count 43 if count <print (" User % s does not exist, please enter again, there are % d chances "% (name, 2-count) 45 count + = 146 else: 47 sys. exit ("User % s does not exist, exit" % name) 48 # Close opened file 49 lock_file.close () 50 user_file.close ()

 

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.