Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the if...elif nesting method in Python

Shulou Source: shulou.com Published: 2022-05-31 10:45:29 10月01日 Update

Most people do not understand the knowledge points of this article "how to use the if...elif nesting method in Python", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the if...elif nesting method in Python" article.

Nested if...elif in if...elif

Mr. Wang Si, we mentioned in the beginning story that he has two secretaries: a life secretary and a work secretary.

Mr. Wang Sizhong is also extremely planned in travel and projects. He assigned a business trip to his life secretary and work secretary respectively:

Life: check the weather first, rain with rain gear and towels. If it doesn't rain, take sunscreen rain gear, towels and sunscreen in advance, if you don't buy it in time.

Work: communicate the work plan before going to Dalian in advance, check in time when you are ready, pass the inspection, and print in advance. If you are not ready in time, list the main project communication issues.

Similar to the case judgment above, we need to use the structure that is repeatedly nested by if...elif...else.

One or more if statements can be nested in if statements to judge multiple parameters, which is the multiple nesting of if statements. Its structure is as follows:

If judgment 1: if judgment 2: code snippet 1 else: code snippet 2 else: if judgment 3: code snippet 3 else: code snippet 4

We use a flowchart to represent it as follows:

Note:

We can add judgment to code snippet 1, 2, 3, 4. Nesting can also be added according to the actual situation.

Note that indentation only makes the code hierarchical, beautiful and easy to read, and has no effect on the generation of the target code.

We put the life requirements of Mr. Wang Sizhong, we can use the code to express the nested structure. We used a three-tier nested structure with the following code:

# coding=utf-8import random# 0 means work secretary, 1 means life secretary # use code to simulate randomly whether the current job is a life secretary or a work secretary, mishu = random.randint (0,1) if mishu: # Rain and no rain Randomly generated # Rain state 1 # No rain state 0 xiyu = random.randint (0Magne1) if xiyu: # whether to buy umbrellas you = random.randint (0L1) if you: print ('on rainy days, you don't need to buy umbrellas') else: print ('rainy days, not purchased Need to buy umbrellas') else: # whether to buy sunscreen you = random.randint (0,1) if you: print ('no rain, there is sunscreen') else: print ('no rain Need to prepare sunscreen') else: # whether the meeting agenda is ready shifou = random.randint (0,1) if shifou: print ('ready, ready to go') else: print ('not ready, need to print, delay departure') these are the contents of this article on "how to use if...elif nesting methods in Python" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Tags: Code secretary work preparation life content sunscreen situation structure method classmate status statement umbrella Wang Si check rainy day multiple that is articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno Huawei MySQL Apple