SoFunction
Updated on 2025-04-08

Solve the ternary operator error "SyntaxError: can''t assign to conditional expression"

Run the code:

a=1
b=1
x=1 if a==b else x=0
print(x)

Tip error:

File "", line 3
 x=a if a==b else x=0
  ^
SyntaxError: can't assign to conditional expression

Expression is an expression, which is an expression connected by various operator symbols such as addition, subtraction, multiplication and division (statement is a statement, such as if statement, while, copy statement, etc.);

Expressions can only be used as lvalues ​​in trilogy operations

After modification:

a=1
b=1
x=1 if a==b else 0
print(x)
[on true] if [expression] else [on false]
#PS: If else expression needs to be complete, example:return com[2] if com[0] == float("-inf") else com[0]