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新手常见错误和异常的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联想发布 GeekPro G5000 游
- 下一篇: 安卓期末项目源码_手机随时随地写Pyth