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