Hey, everyone, good duck , I'm a bear cat
In the use of Python when , As a cute new person, I always carelessly fall here and there , There will be all kinds of errors when running , So write such a blog , To summarize some common mistakes in writing code and solutions .
what are you having? python I won't answer the related error report 、 Or source code information / Module installation / Women's clothing bosses are proficient in skills You can come here :(https://jq.qq.com/?_wv=1027&k=2Q3YTfym) Or ask me at the end of the article
Report errors :
>>> print(a)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
print(a)
NameError: name 'a' is not defined
NameError
Name error
Causes and solutions :
Report errors :
# error 1
>>> a = 1
>>> if a:
print(1)
SyntaxError: expected an indented block
# error 2
>>> if a<b :
SyntaxError: invalid character in identifier
# error 3
>>> print('a)
SyntaxError: EOL while scanning string literal
SyntaxError
Grammar mistakes , Wrong code form
Causes and solutions :
Report errors :
>>> a = list()
>>> a.add('1')
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
a.add('1')
AttributeError: 'list' object has no attribute 'add'
AttributeError
Assignment exception
Causes and solutions :
Report errors :
# error 1
>>>a = input('Enter a number:')
>>>print(a/2)
Enter a number:1
Traceback (most recent call last):
File "C:\Users\acer\Desktop\ test 1.py", line 2, in <module>
print(a/2)
TypeError: unsupported operand type(s) for /: 'str' and 'int'
# error 2
>>> for i in range(1,2,2,3):
print(i)
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
for i in range(1,2,2,3):
TypeError: range expected at most 3 arguments, got 4
TypeError
Type error
Causes and solutions :
Report errors :
>>> a = list()
>>> a.append('1,2,3,a,b');a
['1,2,3,a,b']
>>> a[5]
Traceback (most recent call last):
File "<pyshell#41>", line 1, in <module>
a[5]
IndexError: list index out of range
>>>
IndexError
Index error
Causes and solutions :
Report errors :
>>> a = "abc"
>>> int(a)
Traceback (most recent call last):
File "<pyshell#46>", line 1, in <module>
int(a)
ValueError: invalid literal for int() with base 10: 'abc'
ValueError
Wrong value
Causes and solutions :
The main reason for this error is that the parameter type passed to the object is inaccurate . As in the above example ,a It's a string type , and int() What needs to be passed in is numeric , Therefore, the above error occurred . It's also easy to solve , Just change the type of input value .
Report errors :
>>> d={'a':1,'b':2,'c':3}
>>> d['a']
1
>>> d['f']
Traceback (most recent call last):
File "<pyshell#49>", line 1, in <module>
d['f']
KeyError: 'f'
KeyError
Dictionary key value error
Causes and solutions :
Report errors :
# There is no hello,py This file
>>> f = open('hello.py')
Traceback (most recent call last):
File "<pyshell#54>", line 1, in <module>
f = open('hello.py')
FileNotFoundError: [Errno 2] No such file or directory: 'hello.py'
FileNotFoundError
There are no errors in the file
Causes and solutions :
ps:
How to view python The current path of the interpreter and the files in the directory :
# View directory
import os
os.getcwd()
'C:\\Users\\acer\\Desktop'
# View the files in the directory
os.listdir('C:\\Users\\acer\\Desktop')
#, And the \ The escape of . If there are multiple \ You can also escape through r, namely os.listdir(r’C:\Users\acer\Desktop’) solve .** Remember to use r after , You can't add... At the end of the sentence \
Report errors :
>>> f = open(' test 1.py')
>>> f.write("test")
Traceback (most recent call last):
File "<pyshell#56>", line 1, in <module>
f.write("test")
io.UnsupportedOperation: not writable
io.UnsupportedOperation
An error is reported for the file permission problem ( The above example is used f.write, So it's not writable
Causes and solutions :
The above is Python Some common mistakes in learning .
what are you having? python I won't answer the related error report 、 Or source code information / Module installation /
Women's clothing bosses are proficient in skillsYou can come here :(https://jq.qq.com/?_wv=1027&k=2Q3YTfym) Or ask me at the end of the article