Product Manager learn Python: Conditional Control, product manager python

Source: Internet
Author: User

Product Manager learn Python: Conditional Control, product manager python

The condition control is actually the use of if... else... (if the conditions are true, do...; otherwise, do ...).The basic structure is:

For more information, see the following example:

1 def account_login (): 2 # define function 3 password = input ('Enter the password: ') 4 # enter the password 5 if password = '000000 ': 6 # If the password is 12345, 7 print ('logon successfully') 8 else: 9 print ('wrong password, please try again ') 10 # Otherwise, the system prompts that the password is incorrect. Please re-enter 11 account_login () 12 # re-run function 13 14 account_login () 15 # Call the Function

Define an account logon function. Use the input () function to obtain the user input and store it in the variable password. If the entered password is correct, the system prompts the user to "Log on successfully ", otherwise, the system prompts "the password is incorrect. Please enter it again ".

After the program runs, enter "12345" in the execution result and "Logon successful" is returned. If you enter a number other than 12345, the system will prompt "Incorrect password. Please enter it again ", enter the password again.

Note that a colon must be added after if and else. The front indent refers to the content of this part.

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

Now, we have added the password reset function to the example above:

1 password_list = ['##', '000000'] 2 # create a password list 3 4 def account_login (): 5 password = input (' enter the password :') 6 password_correct = password = password_list [-1] 7 # assign 8 password_correct values to password_reset = password = password_list [0] 9 if password_correct: 10 print ('logon successful ') 11 elif password_reset: 12 new_password = input ('Enter the new password: ') 13 # If the value of password_reset is entered, the user is prompted to enter the new password 14 password_list.append (new_password) 15 # Use the append () method to insert the new password entered by the user into password_list 16 print ('password reset successfully') 17 account_login () 18 else: 19 print ('wrong password, enter ') 20 account_login () 21 22 account_login () 23 24 print (password_list) 25 # to view the latest list content

After the program runs, you will be prompted to enter the password. If you enter "#", the password will be reset, and "Enter new password" will be prompted. After you enter the new password, A new password is inserted at the end of password_list. If the password is entered correctly, "Logon successful" is displayed ".

Print (password_list) in the last line is used to verify whether the new password you reset is inserted into password_list.

In the future, I will introduce the next loop in detail.

Operating Environment: Python version, 3.6; PyCharm version, 2016.2; Computer: Mac

----- End -----

Author: du wangdan, Public Account: du wangdan, Internet product manager.

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.