May 31, 2021 Article blog
Many small partners, in the process of learning Python, met the BUG when busy, do not know how to be good, today's small editor here to summarize some common BUG types and their response measures, I hope the small partners carefully read to do a good note.
Error Type 1: Grammar error
i = 1
while True :
i += 1
if i == 100 :
return
Error:
SyntaxError: 'return' outside function
It is not difficult to see from the code that this little partner wants to perform a loop body that stops the calculation and returns when the value of i is equal to 100, and return can no longer be used outside the method.
Solution:
i = 1
while True :
i += 1
if i == 100 :
break
Error Type 2: Type Error
Name = 'old king'
age = 50
Print ('I am' + Name + ', age' + agn)
Error:
TypeError: must be str, not int
As you can see from the code, the small partner wants to make a combined output of the name and age, but when you're stitching, you have to use a string, and the age is the int type.
Workaround: Turn int type data into strings.
Name = 'old king'
age = 50
Print ('I am' + Name + ', age' + str (agn))
Error Type 3: Grammar error
Name = 'old king'
if Name = 'Old King':
print('Hello')
Error:
SyntaxError: invalid syntax
This seems to be to determine whether name is 'Little King' and if it is 'Hello', but ignores that the ''' symbol is an assignment, and ''' is the comparator.
Solution:
Name = 'old king'
if Name == 'Old King':
print('Hello')
Error Type 4: Indentation error
Name = 'old king'
for index in range(10):
if Name == 'Old King':
print('Hello')
else:
print('No body')
Error:
IndentationError: unindent does not match any outer indentation level
This type of error reporting is more common, and it should not be, mainly because of carelessness and neglect of the syntax indentation.
Workaround: Use 4 spaces for indentation, or tab for automatic indentation.
Name = 'old king'
for index in range(10):
if Name == 'Old King':
print('Hello')
else:
print('No body')
Error Type 5: Index error
text = 'hello world'
print(text[11])
Error:
IndexError: string index out of range
This type of error is out of range for the string to be extracted, remembering that the string subscript counts from 0 instead of 1, and that the spaces within the string are counted.
Error type 6: Value error
text = 'Hello World'
Result = text.index ('Hello')
print(result)
Error:
ValueError: substring not found
This type error is when the user wants to find a string in a string that does not exist, and when looking for a string, it first confirms that the original string has the substrings we need.
Error Type 7: Index error
List1 = ['Dijia', 'Saiwen', 'Cecro', 'Es']
print(list1[4])
Error:
IndexError: list index out of range
The user wants to take out the information for the last location in the list, but the value is outside the list range or the list does not exist, remembering that the underscript of the list is also counted from 0.
Solution:
List1 = ['Dijia', 'Saiwen', 'Cecro', 'Es']
#method one
print(list1[3])
#Method Two
print(list1[-1])
Error Type 8: Property error
tp1 = ('to', 'be', 'or', 'not', 'to', 'be')
tp1.remove('to')
Error:
AttributeError: 'tuple' object has no attribute 'remove'
You can see that the user wants to delete the tuple's 'to' information, but ignores that the tuple does not have a remove method.
Workaround: Delete the tuples after they are converted to a list.
tp1 = ('to', 'be', 'or', 'not', 'to', 'be')
list1 = list(tp1)
list1.remove('to')
Error Type 9: Key error
dic1 = {
'Name': 'Old King',
'age':50,
'Friend': ['Grandson', 'Confucius', 'Laozi', 'Dijia']
}
print(dic1['height'])
Error:
KeyError: 'height'
Key key error: there is no specified key value 'height', from the user-defined dictionary it is not difficult to see that there is no 'height' key, and the user is like taking the value from which there is no, it is inevitable to report the wrong.
Error Type 10: Type Error
dic1 = {
'Name': 'Old King',
'age':50,
'Friend': ['Grandson', 'Confucius', 'Laozi', 'Dijia']
}
dic1.pop()
Error:
TypeError: pop expected at least 1 arguments, got 0
At least one argument needs to be passed in when using the pop() method, but the user does not pass the argument.
The above is a small compilation for everyone to summarize several common BUG types, especially for beginners, should be well pondered, how to according to the type of error, analysis and modification to improve their own written code, as long as willing to heart, these BUGs are not enough teeth!!!
Good lesson recommendations: Python3 Getting Started, Python3 Advanced