首页 👨‍💻计算机,🐍Python

1.未定义

print(a)



    ---------------------------------------------------------------------------
    
    NameError                                 Traceback (most recent call last)
    
    <ipython-input-1-ce747bf5f5c1> in <module>
          1 # 1.未定义
    ----> 2 print(a)


    NameError: name 'a' is not defined


2. 类型不一致

b = 'haha'
c = 123
b+c



    ---------------------------------------------------------------------------
    
    TypeError                                 Traceback (most recent call last)
    
    <ipython-input-2-6dd9b4248729> in <module>
          2 b = 'haha'
          3 c = 123
    ----> 4 b+c


    TypeError: can only concatenate str (not "int") to str


b+str(c)





    'haha123'



c+b



    ---------------------------------------------------------------------------
    
    TypeError                                 Traceback (most recent call last)
    
    <ipython-input-4-baf87dc38fa6> in <module>
    ----> 1 c+b


    TypeError: unsupported operand type(s) for +: 'int' and 'str'


c+int(b)



    ---------------------------------------------------------------------------
    
    ValueError                                Traceback (most recent call last)
    
    <ipython-input-5-362903aa1601> in <module>
    ----> 1 c+int(b)


    ValueError: invalid literal for int() with base 10: 'haha'


int('8')





    8



3.语法错误

符写错了

print(123)



      File "<ipython-input-10-6c2e7a4e109f>", line 1
        print(123)
                 ^
    SyntaxError: invalid character in identifier



漏符号

if 3>2

print(666)


      File "<ipython-input-11-eae8866885e1>", line 2
        if 3>2
              ^
    SyntaxError: invalid syntax



太多符号导致遗漏

int(int(eval('123'))



      File "<ipython-input-12-efb2283c7246>", line 2
        int(int(eval('123'))
                            ^
    SyntaxError: unexpected EOF while parsing



4. 缩进错误 ,出现于 if - 循环 -函数 和 class

if 2>1:

print(2)
 print(3)


      File "<ipython-input-13-f7bbd7cf283e>", line 4
        print(3)
        ^
    IndentationError: unexpected indent



for i in range(5):

print(i)
 print(i+1)


      File "<ipython-input-14-e92bf0843297>", line 3
        print(i+1)
        ^
    IndentationError: unexpected indent



5.索引错误

list2 = [1,2,3]
print(list2[3])



    ---------------------------------------------------------------------------
    
    IndexError                                Traceback (most recent call last)
    
    <ipython-input-15-a7f83310c6d7> in <module>
          1 # 5.索引错误
          2 list2 = [1,2,3]
    ----> 3 print(list2[3])


    IndexError: list index out of range


6. NoteType错误

一般发生在我们连写的时候

dict2={'a':'12345','B':'45678'}
dict2.get('a')[-1]
print(dict2.get('b')[-1])



    ---------------------------------------------------------------------------
    
    TypeError                                 Traceback (most recent call last)
    
    <ipython-input-19-e0eb296646bd> in <module>
          3 dict2={'a':'12345','B':'45678'}
          4 dict2.get('a')[-1]
    ----> 5 print(dict2.get('b')[-1])


    TypeError: 'NoneType' object is not subscriptable


dict2={'a':'12345','B':'45678'}
print(dict2.get('b').find_all('img'))



    ---------------------------------------------------------------------------
    
    AttributeError                            Traceback (most recent call last)
    
    <ipython-input-20-34422fb2b932> in <module>
          1 dict2={'a':'12345','B':'45678'}
    ----> 2 print(dict2.get('b').find_all('img'))


    AttributeError: 'NoneType' object has no attribute 'find_all'


7.文件关闭,继续写入

with open('1.txt','w') as f:

f.write('123')

f.write('456')



    ---------------------------------------------------------------------------
    
    ValueError                                Traceback (most recent call last)
    
    <ipython-input-22-60578682786b> in <module>
          2 with open('1.txt','w') as f:
          3     f.write('123')
    ----> 4 f.write('456')


    ValueError: I/O operation on closed file.


8. 权限错误

csv,excel被 打开的时候,继续写入就会出现

Permissions error




文章评论

目录