If 語句行收到語法錯誤 (Syntax Error received on If statement line)


問題描述

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 SBF12345Eduard Voiculescuamreshk005)

參考文件

  1. Syntax Error received on If statement line (CC BY‑SA 2.5/3.0/4.0)

#syntax-error #Python






相關問題

SQL Server 2008 - 嘗試編輯日期 - 語法錯誤? (SQL Server 2008 - Trying to edit dates - Syntax Error?)

將 Jquery.min 升級到 Jquery.1.9 時出錯 (Error Upgrade Jquery.min into Jquery.1.9)

SyntaxError:無效語法(打印功能) (SyntaxError: invalid syntax (print function))

錯誤:“if”之前的預期表達式 (error: expected expression before ‘if’)

導致 C2061 的用戶創建的標頭:語法錯誤:標識符“類名” (User-Created Header Causing C2061: Syntax Error : Identifier 'classname')

If 語句行收到語法錯誤 (Syntax Error received on If statement line)

語法錯誤:到達了意外的文件結尾。你有一個未關閉的#if (Syntax error: Unexpected end of file reached. You have an unclosed #if)

已經放 } 但錯誤仍然說 } 是預期的? (Already put } but the error still says that } is expected?)

為什麼 Java 不允許在這裡使用三元運算符? (Why doesn't Java allow the use of a ternary operator here?)

我在繪製向量時遇到語法錯誤 (I'm encountering a syntax error while plotting vectors)

我想在 Kotlin 中自定義新按鈕時遇到問題 (I have a problem when I want to customize my new button in Kotlin)

為什麼帶註釋的變量不能是全局的? (Why can't an annotated variable be global?)







留言討論