Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the branch structure of Python Day04

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What is the branch structure of Python Day04? in view of this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Day03-the application scenario of branch structure

So far, the Python code we have written is executed sequentially one statement at a time, and the code of this structure is called a sequential structure. However, sequential structure alone can not solve all the problems. For example, when we design a game, the clearance condition of the first level of the game is that the player gets 1000 points. Then after completing the game, we have to decide whether to enter the second level or tell the player whether to enter the second level or tell the player that the game is over. There will be two branches, and only one of these two branches will be executed, that is, the branch structure in the program. There are many similar scenarios, give you a minute, you should be able to think of at least 5 such examples, give it a try.

The use of if statements

In Python, the if, elif, and else keywords are used to construct the branch structure. Keywords are words with special meanings, such as if and else are keywords specifically used to construct branch structures, and obviously you can't use them as variable names (in fact, they can't be used as other identifiers, either). The following example demonstrates how to construct a branch structure.

"" user authentication Version: 0.1Author: Luo Hao "" username = input ('Please enter the user name:') password = input ('Please enter the password:') # if you want to enter the password, there is no echo in the terminal that can use the getpass function of the getpass module # import getpass# password = getpass.getpass ('Please enter the password:') if username = 'admin' and password = =' 123456please: print ('body) Authentication succeeded!) else: print ('authentication failed!')

The only thing that needs to be noted is that, unlike languages such as Python and Java, curly braces are not used to construct code blocks, but indentation is used to set the hierarchical structure of the code. If the if condition holds, multiple statements need to be executed, as long as multiple statements have the same indentation. In other words, continuous code belongs to the same code block if it maintains the same indentation. It is equivalent to an executive whole.

Of course, if you want to construct more branches, you can use if... Elif... Else... Structure, such as the following piecewise function evaluation.

"" piecewise function evaluates 3x-5 (x > 1) f (x) = x + 2 (- 11) f (x) = x + 2 (- 1)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report