Getting Started with Python (c) Judgment statement

Source: Internet
Author: User

Common judgment statements in Python If....elif....else,while
If
use of if:

if  + 条件判断:     逻辑操作.....

Example:
Let's say you enter a number to determine the size of the number.

#如果这个数字大于80    if 90>80:        print "great"

If the number entered is greater than 80, then the output great

Else's usage:
Conditions are not tenable
Example:

#如果这个数字大于80if 70>80:    print ("great")#如果不大于80else:    print ("it‘s wrong")

We enter a number, if greater than 80, then the output is great, if less than 80, then it is wrong

Usage of elif:
Multiple Judging conditions
Example:

#如果这个数字大于80        if 77>80:            print ("great")#如果这个数字大于60        elif 77>60:             print ("yes")        else:            print ("it‘s wrong")

When I enter a number, such as input 90, then the result of the printing is great, if you enter 77, then the result of printing is yes, when I enter 50, the result of the printing is it ' wrong

The above is the normal judgment and input, but when I need to input it myself?
a=input("please input a number:" )
This is a string for us to enter, we need to cast this string to an integer type, the int
As mentioned earlier, integer type int, string str, floating-point number Floa

a=input("please input a number:" )        if int(a)>80:            print ("great")        elif int(a)>60:             print ("yes")        else:             print ("it‘s wrong")

Question 1:
Input is a string, for example, a space is entered, the space is not an integer
Then we need to determine whether the input has no spaces, with the strip () function

a=input("please input a number:" )    if a.strip():            if int(a)>80:                print ("great")            elif int(a)>60:                 print ("yes")            else:                print ("it‘s wrong")    else:        print("不允许输入空格")

If you enter a space at this time, you will print ' do not allow input spaces '
Question 2
When I enter a bunch of English? Then we continue to add a judgment to judge whether it is a number with a function. IsDigit ()

    1. Judging is not the number isdigit ()
    2. Judging is not the letters and numbers isalnum ()
    3. Judging is not the letter isalpha ()
    4. Judging is not a space isspace ()
    5. Judging is not lowercase/uppercase islower ()/isupper ()
      A=input ("Please input a number:")
      If A.strip ():
      If A.isdigit ():
      if Int (a) >80:
      Print ("Great")
      elif Int (a) >60:
      Print ("Yes")
      Else
      Print ("It ' s Wrong")
      Else
      Print ("This is not a number")
      Else
      Print ("Do not allow input spaces")

While loop
Usage:

while 条件:                             1    逻辑判断if...else..               2

Execution order 1→2→1→2 ..... Until the while condition is not established
Like what

a=1while a<10:    print ("{0}".format(a)    a +=1

So the result of the run is, A=1, when a<10, print a, then a+1, go back to the loop (when a equals to the previous round of a+1), whether the condition a<10 is met, and then print the current "a", then "a" +1, continue back to the loop ....

123456789

Note that when the condition of the while is judged as while True:, or while 1: The loop is a dead loop because the condition is displayed correctly regardless of how the logical judgment is performed
While 1: than while True: execution is fast, the bottom code of the machine is 0 and 1,true need to be converted

Getting Started with Python (c) Judgment statement

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.