Product Manager Learn python: Conditional control

Source: Internet
Author: User

condition control is actually if...else ... (If ... The condition is set up, just do ... On the contrary, do sth. ) , the basic structure of the application is:

See the following example:

1 defAccount_login ():2     #Defining Functions3Password = input ('Please enter your password:')  4     #Enter Password5     ifPassword = ='12345':  6         #If the input password is 12345, the login succeeds7         Print('Login Successful')8     Else:9         Print('incorrect password, please re-enter')  Ten         #Otherwise prompt password is wrong, please re-enter One Account_login () A         #run the function again after entering the error -  - Account_login () the #calling Functions

Define an account login function, use the input () function to get the user input and store in the variable password, if you enter the correct password, prompt the user "login Success", otherwise prompted "password is wrong, please re-enter".

After the program runs, enter "12345" in the execution result, return "login Success", enter a number other than 12345, will prompt "Wrong password, please re-enter", you can enter the password again.

Note that after the IF and else must be preceded by a colon, the preceding indentation refers to which piece of content the part belongs to.

In addition to the above single-condition control, multi-conditional judgment is relatively simple, only need to add elif between If...else, usage and if...else similar.

Now, let's add a password reset function to the example we just made:

1Password_list = ['##','12345']2 #Create a password list3 4 defAccount_login ():5Password = input ('Please enter your password:')6Password_correct = Password = = Password_list[-1]7     #assigning values to Password_correct8Password_reset = Password = =Password_list[0]9     ifPassword_correct:Ten         Print('Login Successful') One     elifPassword_reset: ANew_password = input ('Please enter a new password:') -         #If you enter a value of Password_reset, prompt the user to enter a new password - password_list.append (New_password) the         #Use the Append () method to insert a new password entered by the user into the Password_list -         Print('Password Reset succeeded') - Account_login () -     Else: +         Print('incorrect password, please re-enter') - Account_login () +  A Account_login () at  - Print(password_list) - #You can view the latest list content

After running the program will be prompted to enter a password, if you enter "# #", this will enter the password reset link, will prompt "Please enter a new password", enter a new password, the end of the password_list will be inserted in the new password, the password entered correctly, will show "login success."

The last line of print (password_list) is to verify that the new password that you reset is inserted into the password_list.

I'll cover the next loop in detail later.

Operating environment: Python version, 3.6;pycharm version, 2016.2; PC: Mac

-----End-----

Du Wangdan, public number: Du Wangdan, Internet Product Manager.

Product Manager Learn python: Conditional control

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.