Branch structure
Scenarios
So far, we write Python code is a statement in the order of execution, the structure of the code that we call sequential structure. However, only the sequence structure does not solve all the problems, such as we design a game, the game first hurdle clearance condition is the player get 1000 points, then after the completion of this Council game we have to get points according to the player to decide whether to enter the second off or tell the player "Game Over", where it will have two branches, but only one of these two branches will be executed, this is the program branch structure.
Use 1.if statement
In python, to construct a branch structure can be used if, elif and else keywords.
Exercise: user authentication
Code:
It should be noted that the other language is different, python did not use curly braces to construct the code but the use of the advanced ways to set up a hierarchy of code, if if
Of course, if you want to construct a more branches, may be used if…elif…else…
the structure, for example the following piecewise function evaluation.
Code:
3. Calculator
Code:
Operational results:
4. A rock-scissors game
Code:
5. Enter the length of the three sides of the triangle is determined whether, if the calculation of perimeter and area
Code: