Day4 job code exercises, day4 job code exercises

Source: Internet
Author: User

Day4 job code exercises, day4 job code exercises

Logon module:

Regardless of the website, we often encounter this situation. Let's log on to this website. The flowchart is as follows:

  

Ideas:

1. when we log on to the website, we first enter the user name. At this time, some websites will remind us whether the user name exists. If the user name we enter does not exist, a prompt will appear, this tells us that the user name does not exist. At this time, we need to re-enter the user name or choose to register. Of course, we only need to re-enter the user here;

2. if the user name exists, you need to enter the password. We know that when you enter the password, we do not verify that the password is correct. If you directly verify that the password is correct, the function of letting the user enter the verification code is lost, so we always let the user enter the verification code instead of verifying the password first, and first verify whether the verification code entered by the user is correct;

3. if the verification code is incorrect, ask the user to re-enter the verification code; if the verification code is correct, then return to re-verify the password is correct; if the password is correct, the login is successful; otherwise, if the password is incorrect, you need to re-enter the password. Because the user has already entered the user name, you do not need to re-enter the user name, you only need to enter the password and verification code. That is, re-enter the password, and then enter the verification code. If the password is correct, the logon is successful. Otherwise, re-enter the password and verification code, the verification code is required for each password input;

To implement the above Code function, you must use the while loop. Because it is layer-by-layer verification, the user name input must be a loop until the user enters the correct verification code. The key is, if the user name is correct, you do not need to re-enter the user name when the password or verification code is incorrect. In this case, when the user name is correct, it is about to stop this loop. You cannot enter the user name repeatedly when the next user enters the password or verification code.

The code is implemented as follows:

  

Def login (name, password): ''' User Logon module, various User Logon Settings. During this implementation, generally, the system first checks whether the verification code is correct. Therefore, you must first verify the verification code ''' active = True while True: while active: username = input ("Enter your username :") ''' first, verify whether the user name exists ''' users = [] for user_list in userfile. usernames: users. append (user_list [0]) if username in users: active = False else: print ("sorry, the user name you entered does not exist. Please enter it again :") pwd = input ("enter your password:") while True: ''' Add a module for the user to enter the verification code. '''Verification_code = str (random. randint (0, 9) + chr (random. randint (65,90) + str (random. randint (0, 9) + chr (random. randint (97,122) print (verification_code) test_num = input ("Enter the Verification Code:") if test_num = verification_code.lower () or test_num = verification_code.upper (): ''' whether the user input is case sensitive or not, the user can be verified successfully ''' after the user authentication is successful, start the user's own authentication, whether to register, or the user name is correct ''' break else: print ("the verification code you entered is incorrect. Please enter it again! ") If [username, int (pwd)] in userfile. usernames: print (" Successful! ") Return (username, pwd) else: print (" sorry, the password you entered is incorrect. Please enter it again :")

Run the preceding Code as follows:

Enter your Username: geng, enter your password: 1233D0t, enter the verification code: 3d0t. Sorry, the password you entered is incorrect. Please enter your password again: 2228I9z enter the verification code: 8888 the verification code you entered is incorrect. Please enter it again! 22.16j enter the verification code: 8888 the verification code you entered is incorrect. Please enter it again! For 7T1a, enter the verification code 7t1a. Sorry, the password you entered is incorrect. enter your password 22227X4q again. Enter the verification code 7x4q. Sorry, the password you entered is incorrect, enter your password 6662O0q again. Enter the verification code 2o0qSuccessful! ('Geng', '000000 ')

The execution result of the above Code is as shown above. When we want to stop a loop separately, we can set an identifier for this loop to enable or disable the loop. when conditions are met, close this loop so that it will not be enabled when other programs are executed;

Active = True
While True:
While active:
Username = input ("Enter your username :")
''' First, verify that the user name exists '''
Users = []
For user_list in userfile. usernames:
Users. append (user_list [0])
If username in users:
Active = False
Else:
Print ("sorry, the user name you entered does not exist. Please enter it again :")
Pwd = input ("enter your password :")

The above code implements this function. When we want to stop this loop, We will disable the identifier of the inner loop.

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.