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