日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中冒号报错_python新手常见错误和异常

發布時間:2023/12/19 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中冒号报错_python新手常见错误和异常 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在python編程中,新手最常見的錯入和異常如下

1.缺少冒號引起的錯誤

在if,elif,for,while,class,def聲明末尾需要添加冒號(:),如果忘記添加,將會提示:“SyntaxError: invalid syntax”語法錯誤。例如:

1

2

3

4

5

6

>>> if x > 3

print("x > 3 is ture")

File "", line 2

print("x > 3 is ture")

^

SyntaxError: invalid syntax

2.將賦值運算符(=)和比較運算符(==)混淆

如果誤將=號用作==號,將會提示“SyntaxError: invalid syntax”語法錯誤,列如:

1

2

3

4

5

>>> if x = 3:

File "", line 1

if x = 3:

^

SyntaxError: invalid syntax

3.代碼結構縮進錯誤

這是比較常見的錯誤。當代碼結構的縮進不正確時,常常會提示錯誤信息如:IndentationError: expected an indented block 例如:

1

2

3

4

5

6

7

>>> x = 3

>>> if x > 3 :

... print(" x > 3 is ture")

File "", line 2

print(" x > 3 is ture")

^

IndentationError: expected an indented block

4.修改元組和字符串的值報錯

元組和字符串的值是不能修改的,如果修改他們的元素值將會提示錯誤信息。例如:

1

2

3

4

5

6

>>> tup1 = (12,13)

>>> #以下修改元組元素操作是非法的

... tup1[0] = 100

Traceback (most recent call last):

File "", line 2, in

TypeError: 'tuple' object does not support item assignment

5.連接字符串和非字符串

如果將字符串和非字符串連接,將會提示錯誤“TypeError: can only concatenate str (not “int”) to str”

1

2

3

4

5

6

>>> s = "I love Pyhton"

>>> m = 18

>>> print(s + m)

Traceback (most recent call last):

File "", line 1, in

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

6.在字符串首尾忘記加引號

字符串的首尾必須添加引號,如果沒有添加,或者沒有成對出現,則會提示錯誤“SyntaxError: EOL while scanning string literal”例如:

1

2

3

4

5

>>> print("I love Python)

File "", line 1

print("I love Python)

^

SyntaxError: EOL while scanning string literal

7.變量或者函數名拼寫錯誤

如果函數和變量拼寫錯誤,則會提示錯誤“NameError: name ‘ab’ is not defined”

1

2

3

4

5

>>> aa = "Learn Python"

>>> print(ab)

Traceback (most recent call last):

File "", line 1, in

NameError: name 'ab' is not defined

8. 應用超過列表的最大索引值

如果引用超過列表的最大索引值,則會提示“IndexError: list index out of range”

1

2

3

4

5

>>> aa = [12,13,14,45 ]

>>> print(aa[4])

Traceback (most recent call last):

File "", line 1, in

IndexError: list index out of range

9.使用關鍵字作為變量名

Python 關鍵字不能用作變量名。Python3 的關鍵字有:[‘False’, ‘None’, ‘True’, ‘and’, ‘as’,

‘assert’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘finally’,’for’, ‘from’, ‘global’, ‘if’, ‘import’,’in’, ‘is’, ‘lambda’, ‘nonlocal’, ‘not’, ‘or’, ‘pass’, ‘raise’, ‘return’, ‘try’,’while’, ‘with’, ‘yield’]當使用這些關鍵字作為變量時,將會提示錯誤“SyntaxError: invalid syntax”例如:

1

2

3

4

5

>>> except = [12,13,13]

File "", line 1

except = [12,13,13]

^

SyntaxError: invalid syntax

10.變量沒有初始值就使用增值操作符

如果變量沒有指定一個有效的初始值就使用自增操作符,則會提示錯誤“NameError: name ‘obj’ is not defined”例如:

1

2

3

4

>>> obj += 15

Traceback (most recent call last):

File "", line 1, in

NameError: name 'obj' is not defined

11.誤用自增和自減運算符

在python編程語言中,沒有自增(++)或者自減(–)運算符。如果誤用,則會提示錯誤“SyntaxError: invalid syntax”例如:

1

2

3

4

5

6

>>> jj = 10

>>> jj ++

File "", line 1

jj ++

^

SyntaxError: invalid syntax

12.忘記為方法的第一個參數添加self參數

在定義方法時,第一個參數必須是self。如果忘記添加self參數,則會提示錯誤“TypeError: myMethod() takes 0 positional arguments but 1 was given”例如:

1

2

3

4

5

6

7

8

9

>>> class myClass():

... def myMethod():

... print("this is a good method")

...

>>> dd = myClass()

>>> dd.myMethod()

Traceback (most recent call last):

File "", line 1, in

TypeError: myMethod() takes 0 positional arguments but 1 was given

總結

以上是生活随笔為你收集整理的python中冒号报错_python新手常见错误和异常的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。