問題描述
If 語句行收到語法錯誤 (Syntax Error received on If statement line)
我在編輯器中收到一個錯誤(在運行 py 文件後再次在控制台中,在控制台中描述為 SyntaxError)在包含 If 語句的行上
下面的代碼是包含在文件中
if a = 4:
b = 1
if a = 4:
b = 1
if a = 4:
b = 1
代碼本身是多餘的,但錯誤僅在第一行顯示(在編輯器中),而不是其他行。當其他代碼相同時,僅在第一行代碼上導致此錯誤的原因是什麼?
參考解法
方法 1:
The if statement evaluates a boolean expression. if a = 4
is not a boolean expression in python. You have to use the ==
symbol. Try this:
if a == 4:
b = 1
if a == 4:
b = 1
if a == 4:
b = 1
方法 2:
Comparison for equality is done using the == operator (you're using a single = which is for assignments only). Also, you're missing a colon:
if result == "PASS":
(by SBF12345、Eduard Voiculescu、amreshk005)