Acquaintance python - Process Control if ... else

Process Control

If the writing process likened to walking, then we up to now, has always followed a straight road, have not met too Crossings, imagine the real world, you encounter a fork in, and then you decide where to turn there must be the motives. You have to judge your true piece fork is the way to go if we want the program to be able to handle such a decision how to do? Very simple, just a preset number of conditional statements in the program, which conditions are met, which left fork in the road. This process is called flow control.

Substantially in each language, the syntax used is achieved if ... else ..., can be divided into single branch, two branches, multi-branch

Single branch

  1. if 条件:
  2. 满足条件后要执行的代码

Dual Branch

  

if the condition: 
the condition code execution 
the else : 
if this condition is not satisfied and left 
Age = 18 is
 if Age> 22 is :
 Print ( "BYE BYE " )
 the else :
 Print ( "Cool! " )

 

indentation


Here it must be inserted into the indent of knowledge

You will find, if the above code, the next row in each condition indented four spaces, which is why? This is a major feature of Python, forcing indentation, the purpose is to let the program know that each piece of code depends on what conditions, if not through indentation to distinguish how the program will know when your condition is met, what code to execute it ?

Python is a door super simple language, the inventor is given a feel {}ugly, so simply do not direct it, how to distinguish between code block it? The answer is mandatory indentation.

Python indentation has the following principles:

    • The top line of top-level code must be written, that is, if a line of code itself does not depend on any conditions, then it must not be any indentation
    • The same level of code indentation must be consistent
    • The official suggested indent with four spaces, of course, you can also use two, if you want to be a joke to say.

Multi-Branch

Back up control process, if ... else ... you can have multiple branch condition

  

if conditions: 
the condition code execution 
elif condition: 
the above condition is not satisfied and left the 
elif condition: 
the above condition is not satisfied and left the 
elif condition: 
the above condition is not satisfied and left the 
the else : 
all of the above conditions are not satisfied and left this

 

Write a guessing game now age

  

= 48 age_of_oldboy 
GUESS = int (the INPUT ( " >>: " ))
 IF GUESS> age_of_oldboy:
 Print ( " guess too much, to try in a small ... " )
 elif GUESS < age_of_oldboy:
 Print ( " guess too small to Dali try ... " )
 the else :
 Print ( " Congratulations, you guessed it ... " )

 

Examples of the above, depending on the values ​​you entered, will be up to 3 different results


At this point let the students themselves dictation again this code

Again a small program results matching it, the results have ABCDE5 levels, correspondence between the scores are as follows

  1. A 90-100
  2. B 80-89
  3. C 60-79
  4. D 40-59
  5. E 0-39

After users enter numbers from 0 to 100, you can print the results corresponding to correct his

  

int = Score (the INPUT ( " Enter the score: " ))
 IF Score> 100 :
 Print ( " I rub, the highest score before ... 100 " )
 elif Score> = 90 :
 Print ( " A " )
 elif Score> = 80 :
 Print ( " B " )
 elif Score> = 60 :
 Print ( " C " )
 elif Score> = 40 :
 Print ( " D ")
else:
 Print ( " dumb E ... " )

 

The problem here is that when I enter 95, it prints the result is A, but 95 obviously also greater than the second condition elif score >=80:Yeah, why not print it B? This is because the code is judged from top to bottom, as long as one will not gone down, it must be clear ah!

Guess you like

Origin www.cnblogs.com/jhui104/p/11444068.html