Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 20:18:47 10月05日 Update

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!

Tags: Loops statements processing code parameters clips support runs methods phenomena content situations more knowledge differences practicality consistency learning and then anyway Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Huawei Apple Shulou Tech Info vpn