In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to solve Python error report". In daily operation, I believe many people have doubts about how to solve the problem of Python error report. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to solve Python error report"! Next, please follow the editor to study!
There are bound to be errors in writing code, and error handling can be prepared in advance for these errors. Usually when an error occurs, the script stops running, and with error handling, the script can continue to run. To do this, we need to know the following three keywords:
Try: this is the block of code to run and may generate errors.
Except: if an error occurs in the try block, this code will be executed.
Finally: execute this code no matter what error occurs.
Now, let's define a function "summation" that adds two numbers. The function is running normally.
> defsummation (num1,num2): print (num1+num2) > summation (2pr 3) 5
Next, we ask the user to enter one of the numbers and run the function.
> num1 = 2 > num2 = input ("Enter number:") Enter number: 3 > summation (num1 Num2) > print ("Thisline will not be printed because of the error")-TypeError Traceback (most recent call last) in-> 1 summation (num1) Num2) 2 print ("This line will notbe printed because of the error") in summation (num1,num2) 1 def summation (num1,num2):-> 2 print (num1+num2) TypeError: unsupported operand type (s) for +: int and str
The "TypeError" error occurs because we are trying to add a number to a string. Note that after the error occurs, the subsequent code is no longer executed. So we need to use the keywords mentioned above to make sure that even if something goes wrong, the script still runs.
> > try: summed = 2 + 3 except: print ("Summation is not ofthe same type") Summation is not ofthe same type
You can see that there is an error in the try block, and the code for the except block starts running and the statement is printed. Next, add a "else" block to deal with situations where there are no errors.
> try: summed = 2 + 3 except: print ("Summation is not ofthe same type") else: print ("There was no errorand result is:", summed) There was no errorand result is: 5
Let's use another example to understand. In this example, we also indicate the error type in the except block. If the error type is not indicated, any exception will execute the except block.
> try: F = open (test, w) f.write ("This is a testfile") except TypeError: print ("There is a typeerror") except OSError: print ("There is an OSerror") finally: print ("This will print evenif no error") This will print evenif no error
Now, deliberately create an error to see if the except block works with the finally block.
> try: F = open (test, r) f.write ("This is a testfile") except TypeError: print ("There is a typeerror") except OSError: print ("There is an OSerror") finally: print ("This will print evenif no error") There is an OSerror This will print evenif no error, the study on "how to solve Python error reporting" is over. I hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.