Python100Days study notes --- Day3 branch structure

Scenarios
So far, we wrote a Python code is a sequence of statements executed, this code structure commonly referred to as 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 the game after the completion of the Council, we have to decide whether to enter the first player gets points according to two off, or tell the player "Game Over", where it will have two branches, but only one of these two branches will be executed. There are many similar scenes, we will call this structure "branch structure" or "choice architecture." Give us a minute, you should be able to think of at least five such examples, and quickly try.

Use the if statement
in Python, to construct a branch structure can be used if, elif and else keywords. The so-called key word is to have special meaning, like if and else is specifically constructed for keywords branched structure, it is clear that you can not use it as a variable name (in fact, used for other identifiers is not allowed). The following example demonstrates how to construct a branch structure.

"""
用户身份验证

Version: 0.1
Author: 骆昊
"""

username = input('请输入用户名: ')
password = input('请输入口令: ')
# 用户名是admin且密码是123456则身份验证成功否则身份验证失败
if username == 'admin' and password == '123456':
    print('身份验证成功!')
else:
    print('身份验证失败!')

The only explanation is and C / C ++, Java, etc. in different languages, Python does not use braces to construct a block of code but the use of indentation way to set up a hierarchy of code, in the case if the condition is satisfied if you need to perform more than statement, as long as they have the same plurality of statements will be indented, in other words, if successive code while maintaining the same indentation then they belong to the same block, the equivalent of a whole is performed.

Of course, if you want to construct a more branches, may be used if ... elif ... else ... structures, for example the following piecewise function evaluation.

Here Insert Picture Description

"""
分段函数求值

        3x - 5  (x > 1)
f(x) =  x + 2   (-1 <= x <= 1)
        5x + 3  (x < -1)

Version: 0.1
Author: 骆昊
"""

x = float(input('x = '))
if x > 1:
    y = 3 * x - 5
elif x >= -1:
    y = x + 2
else:
    y = 5 * x + 3
print('f(%.2f) = %.2f' % (x, y))

Of course, according to actual development of the branched structure it can be nested, for example, given a rating is determined also on whether your performance after the number you get clearance or treasures props (such as two or three lighting stars), then we need to construct the inside if a new branch structure, and empathy elif else may be re-configured new branch, which we call nested branched structure, i.e. the above code can be written as follows look.

"""
分段函数求值
		3x - 5	(x > 1)
f(x) =	x + 2	(-1 <= x <= 1)
		5x + 3	(x < -1)

Version: 0.1
Author: 骆昊
"""

x = float(input('x = '))
if x > 1:
    y = 3 * x - 5
else:
    if x >= -1:
        y = x + 2
    else:
        y = 5 * x + 3
print('f(%.2f) = %.2f' % (x, y))

Note: you can feel yourself both written in the end is which one is better. Council may have serious implications code in Python Zen Before we mentioned in so many words "Flat is better than nested." , The reason to promote the code "flat" because the nesting level of the nested structure after more of reading, so do not use nested when flattened structure can be used.

Here Insert Picture Description

Here Insert Picture Description

Here Insert Picture Description

Published 124 original articles · won praise 141 · views 160 000 +

Guess you like

Origin blog.csdn.net/weixin_36838630/article/details/105205767
Recommended