Conditional statement: Used to determine whether a condition is met, compliance is executed, non-conforming does not perform the operation defined by the condition.
One-step decision: Used to understand this will not be used.
The If 1==1:if condition is judged to occur only once.
Print ("1") here the print is relative to if there are four spaces or a TAB key indentation, meaning that print is part of the IF code.
1==1 is a condition of fixation and death, and usually we use dynamic conditions instead of writing dead conditions, and this is for illustrative purposes only. You must add the condition after the IF condition is
To pass the Boolean operation, the result of the operation is true, then execute the subsequent code in the conditional statement, which is print. It is obvious that the above code 1==1 this
The equation is right, that is, the Boolean operation is true-true, so print is executed. If the conditional Boolean value 1==1 here does not satisfy true, then the print
will not be executed. For example, replacing 1==1 with 1>1 1>1 will get false after a Boolean operation.
Two-step decision:
Print ("error")
Here the code first runs the above if, if the condition after the if is true, then the print is correct, and else is not executed. If the condition after the if is not satisfied
Skip the If code snippet to do else directly.
Multi-Criteria Two-step decision:
else: #else print ("error")
① If there are two conditions after the if, and the and is used, then that means that both conditions must be true after the Boolean operation to perform print correctly.
No , else print error is performed.
②and is both two sides of the same time to meet, or is as long as one can meet.
Multi-Step decision:
Name = input (">>>: ") #input函数自动赋值 if name = = Span style= "COLOR: #800000" > "admin" : Print (" admin login " Span style= "COLOR: #000000" >) elif name = = "guest ": #elif可以无限次出现 print (" guest login else print (" unknown user
The above code first defines a receive function and assigns a value to name, and then in the conditional statement to determine whether the function and the condition satisfies, satisfies the execution, does not satisfy then matches the next conditional statement.
Conditional statement nesting and closing:
User_type = input ("Please enter user type Admin or guest:")if User_type = ="Admin": user_name = input ("Please enter user name:")if user_name = ="Armin": Print ("Welcome Armin") elif user_name = ="Admin": Print ( " welcome admin ") else : Print ( " unknown user " ) elif User_type = = "guest" : Print ( Welcome guest ") else: Print (" Unknown user
If Elif and else first from the three inside to determine the conditions, if one of them, then go into the details of execution. For example here if
If the conditions are met, then it is time to execute if elif and else inside if. Here you see that the first if inside adds some if elif else these are
Practice Questions:
1 using the input getpass in the LEARNING2 and the conditional statements in this tutorial to set a user login, the user name password correctly prompts the login success, error
Prompt for user name or password error.
2 extension exercises can refine multiple user names and passwords, require input a login successful display welcome Administrator B Login Successful display is not displayed user name or password
Error. Welcome user .....
3 The extension exercise first prompts for a category, gives a category hint of the login, and then enters the user name as prompted. User name matching prompt for password, correct password
Give a hint, password error to give a hint and quit the program. Requires multiple user names. Using nested methods
Python 3 Conditional statement