2 回答

TA貢獻1906條經驗 獲得超3個贊
您好,1)忘記在 if , elif , else , for , while , class ,def 聲明末尾添加 :(導致 “SyntaxError :invalid syntax”)
該錯誤將發生在類似如下代碼中:
if spam == 42
print('Hello!')
2)使用 = 而不是 ==(導致“SyntaxError: invalid syntax”)
= 是賦值操作符而 == 是等于比較操作。該錯誤發生在如下代碼中:
if spam = 42:
print('Hello!')
3)錯誤的使用縮進量。(導致“IndentationError:unexpected indent”、“IndentationError:unindent does not match any outer indetation level”以及“IndentationError:expected an indented block”)
記住縮進增加只用在以:結束的語句之后,而之后必須恢復到之前的縮進格式。該錯誤發生在如下代碼中:
print('Hello!')
print('Howdy!')
或者:
if spam == 42:
print('Hello!')
print('Howdy!')
或者:
if spam == 42:
print('Hello!')
4)在 for 循環語句中忘記調用 len() (導致“TypeError: 'list' object cannot be interpreted as an integer”)
通常你想要通過索引來迭代一個list或者string的元素,這需要調用 range() 函數。要記得返回len 值而不是返回這個列表。
該錯誤發生在如下代碼中:
spam = ['cat', 'dog', 'mouse']
for i in range(spam):
print(spam[i])
添加回答
舉報