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

The methods of dealing with abnormal phenomena in the use of Python Loop sentences

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "handling abnormal phenomena in the use of Python loop sentences". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The break statement is used to abort the loop immediately, and the continue statement is used to go directly to the next loop (ignoring the remaining statements of the current loop). The break and continue statements are used in the same way in the C # and Python loop statements, only for the current loop in which the statement is located. If you need to exit a multiple loop, you should use an exception because the goto statement is not provided in Python.

* * the loop in Python also supports the else statement, which runs only after the loop completes normally (for and while loops), or immediately when the loop condition does not hold (only while loops), or when the iteration sequence is empty (only for loops). If the loop exits with the break statement, the else statement will be ignored. The following code is used to illustrate the application of else in the loop.

# while-else while I < 10: ii = I + 1 else: print 'Done' # for-else for an in s: if a =' Foo': break10 else: print 'Not examples'

Abnormal

Python loop statement supports exception handling like C #. Using try/except/finally structure, you can easily catch exceptions. At the same time, you can manually throw exceptions with raise statements (the above four exception handling keywords correspond to try/catch/finally/throw in C #). With except, you can catch errors and exceptions in statements marked by try, except can accept parameters as exceptions to catch, and if you want to catch multiple exceptions, you can use tuple as parameters. An except with no parameters is considered to catch all exceptions. Finally, on the other hand, is used to execute code that must be run in * *, such as recycling. Here is a simple example of how to handle exceptions in Python:

Try: F = open ('thefile.txt') s = f.readline ()... Except IOError, (errno, strerror): print "error O error (% s):% s"% (errno, strerror) except ValueError: print "Could not convert data to an integer." Except: print "Unexpected error:", sys.exc_info () [0] raise finally:f.close ()

To note, the try of the Python loop statement also supports the else statement. If there is some code to be executed without an exception in try, you can put it in else (unlike finally, the finally branch will be executed anyway).

This is the end of the content of "handling abnormal phenomena in the use of Python Loop sentences". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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