The third day of python -- start the job, and the third day of python

Source: Internet
Author: User

The third day of python -- start the job, and the third day of python

Job 1, simulated login:
1. the user enters the account password for login
2. Save User information in the file
3. the user password is locked three times after being entered incorrectly.

File used: user_file.txt user list file. Format: {'zhang san': '123abc', 'lili': 'eeettt', 'wang wu': 'www123', 'calmya': 'yanyan ', 'aelx ': 'tttttt '}

The User File locked by lock_name.txt. Format: ['calmyanc']

First, go to the flowchart:

The following code details:

1 #! Usr/bin/env python 2 #-*-coding: UTF-8-*-3 # Author calmyan 4 # import getpass 5 user_file = open ("user_file.txt", 'R ', encoding = 'utf-8') # Open the user list file 6 lock_name1_open('lock_name.txt ', 'r +', encoding = 'utf-8 ') # Open the locked user list file 7 user_list = eval (user_file.readline () # convert the string 8 lock_list = eval (lock_name.readline () in the User File ()) # convert and lock the string 9 10 def userchar (_ user_name) in the User File: # function 11 for username detection while True: 12 # _ user_name = (input ('enter the username: ') 13 If _ user_name in user_list and _ user_name not in lock_list: # determine whether the user exists and is normal 14 return True # returns True 15 break16 elif _ user_name in user_list and _ user_name in lock_list: # determine whether the user is locked 17 print ('your user has been locked, please contact the merchant to unlock! ') 18 break19 else: 20 print ('the user you entered does not exist, please register first') 21 break22 23 def out (): # related file operation function 24 global bool # define exit loop condition global variable 25 bool = False26 lock_name.close () # Close Lock User File 27 user_file.close () # Close user list file 28 29 def confirm (): # exit the confirmation function 30 qu = input ("whether to log on again! Press \ 033 [31; 1mY/y \ 033 [0 m to log on again; otherwise, exit: ") 31 if qu = 'y' or qu = 'y ': 32 pass33 else: 34 print ("You have exited! ") 35 out () 36 37 bool = True # define exit loop condition variable 38 while bool: 39 _ user_name = (input ('enter Username :')) 40 if userchar (_ user_name): # Run the detection username function to determine 41 count = 3 # define the number of locks 42 while count> 0: # If the number of password errors exceeds the value of exit password, enter 43 # password = getpass. getpass ('enter password: ') 44 password = input ('enter password:') 45 if password = user_list [_ user_name]: # password verification completion cycle 46 print ("password verification passed") 47 out () 48 print ("Welcome \ 033 [32; 1 m % s \ 033 [0 m you have logged on successfully! "% _ User_name) 49 break50 else: 51 count-= 1 # decrease in the number of times 52 print (" Password error please try again, remaining: \ 033 [31; 1 m % s \ 033 [0 m times! "% Count) 53 else: 54 print (" \ 033 [31; 1 Mbit/s exceeds the number of trials, and the account is locked! Contact the seller! \ 033 [0 m ") 55 lock_list.append (_ user_name) # Add the user name to the lock list 56 lock_name.seek (0) # Move the pointer to the beginning, to avoid the generation of multiple tuples 57 lock_name.write (str (lock_list) # Write to lock the User File 58 lock_name.tell () # Get the current pointer, avoid generating multiple tuples 59 # qu = input ("whether to log on again! Press Y to log on again. Otherwise, exit: ") 60 # if qu = 'y': 61 # pass62 # else: 63 # print (" You have exited! ") 64 # out () 65 confirm () 66 continue # Return to the input username location 67 68 else: # If a user name error occurs, 69 new confirm () 70 pass71 72 # print ("Welcome to \ 033 [32; 1 m % s \ 033 [0 m you have logged on successfully! "% _ User_name) 73 # if password = user_list []
View Code

 

After testing, I think there is no problem!

In addition, the prompt function is added. If the user does not store the prompt, the prompt has been fixed, and the remaining number of retry passwords is displayed!

2. You can choose whether to return or not!

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.