Python 3 Conditional statement

Source: Internet
Author: User

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.

  if 1==1: The IF condition is determined 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. If the condition is followed by a Boolean operation and the result of the operation is true, then the subsequent code in the conditional statement is executed, which is print. It is obvious that this equation is 1==1 in the above code, that is, after the Boolean operation is True-true, print is executed. If the conditional Boolean value 1==1 here does not satisfy true, then 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:
If 1 = = 1:
Print ("correct")
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 execute the else directly.

Multi-Criteria Two-step decision:
If 1 = = 1 and 2 > 1:
Print ("correct")
Else: #else can only occur once.
Print ("error")

If there are two conditions followed by, and the and is used, then that means that both conditions must be true after the Boolean operation to perform print correctly. Otherwise, the print error of else is performed.

And is both on both sides to meet, or is as long as one can satisfy.

Multi-Step decision:
name = input (">>>:")#input函数自动赋值
If name = = "Admin":
Print ("Admin login")
elif name = = "Guest": #elif可以无限次出现
Print ("Guest login")
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")
El if user_name = = "Admin":
print ("Welcome Administrator")
print (" Unknown user ")
elif user_type = = "Guest":
print ("Welcome guest")

print ("Unknown user")

The first level above is 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, if the IF condition is met, then the if elif and else in the next step should be executed. Here we see that the first if inside adds some if elif else these are nested.

Practice Questions:

1 using the input Getpass in LEARNING2 and the conditional statements in this tutorial to set up a user login, the user name password is correct prompt login success, error prompt user name or password error.

2 extension exercises can fine-tune multiple user names and passwords, require input a login successful display welcome Administrator B Login Successful display is not correct display 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 prompts to enter a password, the password is correct to give a hint, password error to give a hint and quit the program. Requires multiple user names. Using nested methods

Python 3 Conditional statement

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: 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.