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

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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.

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

Internet Technology

Wechat

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

12
Report