In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about the common BUG types of Python, which are detailed in content and clear in logic. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
Error type 1: syntax error
I = 1
While True:
I + = 1
If I = = 100:
Return
Error report:
SyntaxError: 'return' outside function
It is not difficult to see from the code that the partner wants to execute a loop body, stop calculating and return when the value of I is equal to 100, and return cannot be used outside the method.
Solution:
I = 1
While True:
I + = 1
If I = = 100:
Break
Error type 2: type error
Name = 'Lao Wang'
Age = 50
Print ('I am'+ name +', age'+ age)
Error report:
TypeError: must be str, not int
You can see from the code that the partner wants to make a combined output of name and age, but when doing + concatenation, you must use a string, while the age is of int type.
Solution: convert the int type data to a string.
Name = 'Lao Wang'
Age = 50
Print ('I am'+ name +', age'+ str (age))
Error type 3: syntax error
Name = 'Lao Wang'
If name = 'Lao Wang':
Print ('Hello')
Error report:
SyntaxError: invalid syntax
Here it seems to be judging whether name is' Xiao Wang'. If so, output 'Hello', but ignore the fact that the' = 'symbol is the assignment character and the' = ='is the comparison character.
Solution:
Name = 'Lao Wang'
If name = 'Lao Wang':
Print ('Hello')
Error type 4: indent error
Name = 'Lao Wang'
For index in range (10):
If name = 'Lao Wang':
Print ('Hello')
Else:
Print ('No body')
Error report:
IndentationError: unindent does not match any outer indentation level
This type of error reporting is more common and should not be, mainly due to carelessness and ignoring the indentation of grammar.
Solution: use 4 spaces to represent indentation, or press tab to achieve automatic indentation.
Name = 'Lao Wang'
For index in range (10):
If name = 'Lao Wang':
Print ('Hello')
Else:
Print ('No body')
Error type 5: index error
Text = 'hello world'
Print (text [11])
Error report:
IndexError: string index out of range
This type of error is that the string you want to extract is out of range, remember that the subscript of the string is counted from 0, not from 1, and the spaces in the string are counted.
Error type 6: value error
Text = 'Hello World'
Result = text.index ('Hello')
Print (result)
Error report:
ValueError: substring not found
This type of error is that the user wants to find a string that does not exist in the string. When looking for a string, make sure that the original string has a substring that we need.
Error type 7: index error
List1 = ['Diga', 'Sven', 'Cerro', 'Ace']
Print (list1 [4])
Error report:
IndexError: list index out of range
The user wants to retrieve the information from the last location in the list, but the value is out of the range of the list or the list does not exist, and remember that the subscript of the list is also counted from 0.
Solution:
List1 = ['Diga', 'Sven', 'Cerro', 'Ace']
# method 1
Print (list1 [3])
# method 2
Print (list1 [- 1])
Error type 8: property error
Tp1 = ('to',' be', 'or',' not', 'to',' be')
Tp1.remove ('to')
Error report:
AttributeError: 'tuple' object has no attribute' remove'
You can see that the user wants to delete the 'to' information of the tuple, but ignores that the tuple does not have a remove method.
Solution: convert the tuple to a list and delete it.
Tp1 = ('to',' be', 'or',' not', 'to',' be')
List1 = list (tp1)
List1.remove ('to')
Error type 9: key error
Dic1 = {
'Lao Wang name':''
'age':50
Friend': ['Sun Tzu', 'Confucius', 'Laozi', 'Diga']
}
Print (dic1 ['height'])
Error report:
KeyError: 'height'
Key key error: there is no specified key value 'height', it is not difficult to see from the user-defined dictionary that there is no' height' 'key, but the user seems to take no value from it, it is inevitable to report an error.
Error type 10: type error
Dic1 = {
'Lao Wang name':''
'age':50
Friend': ['Sun Tzu', 'Confucius', 'Laozi', 'Diga']
}
Dic1.pop ()
Error report:
TypeError: pop expected at least 1 arguments, got 0
At least one parameter needs to be passed in when using the pop () method, but the user does not pass the parameter.
These are all the contents of the article "what are the common BUG types of Python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more 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.
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.