Python basic--if Process Control and looping

Source: Internet
Author: User

If...else of Process Control

Since the purpose of our programming is to control the ability of a computer to work like a human brain, what the human brain can do is that it needs to have mechanisms in the process to simulate it. The human brain is nothing more than a mathematical and logical operation, as we have said in the previous section on mathematical operations. For the logical operation, the person makes different reflection according to the change of the external condition.


# # #Python定义了缩进的机制. You need to indent 4 spaces in front of the execution statement when using Process control or looping.


If...else format:


If condition:

EXECUTE statement

Else

EXECUTE statement


For example: Determine the date, if it is Sunday to play games, not Sunday to go to work to sleep.


Date= ' Monday '

If date = = ' Sunday ':

Print (' Play games ')

Else

Print (' Sleep at Work ')


If...elif...else format:


If condition 1:

EXECUTE statement

Elif Condition 2:

EXECUTE statement

Elif Condition 3:

EXECUTE statement

......

Else

EXECUTE statement


For example:

If: Score >=90, then: excellent

If the score >=80 and <90, then: good

If the scores are >=60 and <80, then: normal

Other situation: ... Write casually


# Coding:utf-8 #转成utf-8

Value=int (Input (' Enter this exam result: ') #让用户手动输入. Int () is converted to numeric type

If Value >= 90:

Print (' excellent ')

Elif Value >= 80:

Print (' good ')

Elif Value >= 60:

Print (' normal ')

Else

Print (' Well done ... This result NB Big! ')


While loop

The example above which the test results are entered executes once and exits the program, if you want to keep it in a state of execution. Implementing this requirement requires a loop.


While format:


While condition:

Loop body


Use the last question to do an example. achieve multiple input scores. The program ends until the value entered by the user is not a score (number).


# Coding:utf-8

While True:

Value=int (Input (' Enter this exam result: '))

If value > 100:

Print ("" "

Only numbers can be entered

Range (1-100)

""")

Break

Elif value >= and value <= 100:

Print (' excellent ')

Elif Value >= 80:

Print (' good ')

Elif Value >= 60:

Print (' normal ')

Else

Print (' Well done ... This result NB Big! ')

Python basic--if Process Control and looping

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.