The Nineth section, the basic conditional statement if

Source: Internet
Author: User
Tags ranges

Conditional statements

If we want to respond effectively to the user's input, the code needs to be able to judge. Can let the program to judge the structure becomes the condition, the condition Judgment statement returns the Boolean value TRUE or FALSE, actually executes one line, false executes another line

if Conditional Judgment Statement

  If so, how else?

  = = Usage (if equal to execution of first line)

Such as:

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3NL = Input ("Please enter a verification code")4 ifNL = ="123":5     Print("123 Verification Code is correct")6 Else:7     Print("Verification Code Error")

As listed above, if the user input is 123 then judge the NL variable equals 123 is correct, print the first line, if the user input is not 123 then is wrong, execute the second line

If so, how else?

  ! = Usage (if not equal to execution of first line)

Such as:

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3NL = Input ("Please enter a verification code")4 ifNL! ="123":5     Print("Verification code is correct")6 Else:7     Print("Verification Code Error")

As listed above,! = (if not equal to the meaning), that is, the user input if not equal to 123 to execute the first line verification code is correct, the user input if equal to 123 to execute the second line code error

  Summary: = = with! = The judging condition is the opposite (Key Note)

  and (and)  

  and the use of (that is, to add a judgment condition, and before and after the first line to be executed in accordance with the conditions of judgment, even if a judgment condition does not conform to the implementation of the second line)

  Such as:

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3N1 = input ("Please enter user name")4N2 = input ("Please enter a verification code")5 ifN1 = ="Lin Guixiu"  andN2 = ="123":6     Print("user name and verification code are correct")7 Else:8     Print("incorrect user name or verification code")

As listed above, that is, the user name and verification code to meet the criteria for the first line will be executed, there is a non-compliance with the conditions of judgment, will execute the second line

  or (OR)  

  The use of or (also adds a judgment condition, or or the meaning, that is, before and after the condition to determine that one conforms to the execution of the first line, otherwise execute the second line)

  Such as:

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3N1 = input ("Please enter user name")4N2 = input ("Please enter a verification code")5 ifN1 = ="Lin Guixiu" orN2 = ="123":6     Print("user name or verification code is correct")7 Else:8     Print("incorrect user name and verification code")

As listed above, or or, that is, two to judge one of them in line with the judgment, the first line is executed, of course, two are in line with the implementation of the first line, two are not in line with the implementation of the second line

< (less than) > (greater than) = (equals) multi-way judgment

Set multiple ranges in the judgment, different ranges to perform different lines

  Such as:

  

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3n = 254 ifN <= 10:5     Print("Little Brat .")6 elifn > 10 andN <= 20:7     Print("teenager")8 elifn > 20 andN <= 30:9     Print("It's time to be married")Ten Else: One     Print("your age is not in my area of concern.")

1-10 years old is: Little brat

More than 10 to 20 years old is: teenager

20 years old to 30 years old: It's time to get married.

Over 30 years old is: your age is not within my relationship.

 

  

The Nineth section, the basic conditional statement if

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.