A complete list of Python common errors (Error) - a must-see for beginners

There is a long way to go, and there is a long way to go to learn python. For beginners, the most uncomfortable thing is reporting errors, followed by errors. The translation is done, but it will not be changed. So today, I am an IKUN teacher , Combined with our common mistakes, make a complete list of common mistakes! ! , If you encounter errors in the future, you can search directly in this article!

1、【SyntaxError: xxxxxxxxxxx

This error is a [syntax] error

wrong reason:

1. The brackets are not written

insert image description here

2. The colon is not written

insert image description here

3. There is no space after the keyword:

insert image description here

Note that there are some errors that are themselves syntax errors and cannot be caught.

2、【NameError: name 'name' is not defined

This error is [variable name] error

wrong reason:

1. The variable name has not been created

insert image description here

2. The upper and lower variable names are inconsistent (wrongly written by hand)

insert image description here

3、【IndexError: list index out of range

This error is an [index] error

wrong reason:

1. The index value written exceeds the index of [string, tuple, list, etc.]

insert image description here

2. Additional knowledge 如果是切片超出范围,就不会报错:

returns an empty list

insert image description here

4、【KeyError: xxxxx

This error is [key] error

wrong reason:

1. The value corresponding to the written key cannot be queried in the dictionary!

insert image description here

2. Additional knowledge 如果用get,就不会报错:

If the query cannot be found, it will return None by default. If the query cannot find the return value, it can be customized

insert image description here

custom return value

insert image description here

5、【FileNotFoundError: [Errno 2] No such file or directory: 'xxxxx'

This error is [file not found] error

wrong reason:

1. The path of the file you want to read is wrong

2. The file you want to read does not exist at all

6、【ZeroDivisionError: division by zero

This error is [divisor is 0] error

wrong reason:

1. Your numerator is 0, please check your numerator

7、【UnboundLocalError: local variable 'xx' referenced before assignment

This error is [global variable is declared to be modified inside the function] error

wrong reason:

1. There is a variable outside the function, the global variable is not declared, and the modification is made inside the function, so an error is reported

insert image description here

2. Modify (add global)

insert image description here

8、【AttributeError: 'xxx1' object has no attribute 'xxx2'

This error is [xxx1 object has no attribute called xxx2] error

wrong reason:

1. The function/property called by the object is wrongly written

2. The function/property called by the object does not exist at all

insert image description here

9、【UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: invalid continuation byte

This error is [file decoding] error

wrong reason:

1. If it is UnicodeDecodeError: 'utf-8' codec can't...

Just change encoding='UTF-8' to encoding='gbk

Conversely: just change encoding='gbk' to encoding='utf-8'

insert image description here

After modification:

insert image description here

Hope it helps beginners

A little programmer dedicated to office automation

Hope to get [a free follow] from everyone! grateful

Guess you like

Origin blog.csdn.net/weixin_42636075/article/details/130887941
Recommended