In my code I have these lines:
if numVotes == 0:
avId in self.disconnectedAvIds or self.resultsStr += curStr
When I run the code I get this error?
SyntaxError: illegal expression for augmented assignment
How do I fix this error?
In my code I have these lines:
if numVotes == 0:
avId in self.disconnectedAvIds or self.resultsStr += curStr
When I run the code I get this error?
SyntaxError: illegal expression for augmented assignment
How do I fix this error?
The assignment
self.resultsStr += curStr
is a statement. Statements can not be part of expressions. Therefore, use an if-statement
:
if avId not in self.disconnectedAvIds:
self.resultsStr += curStr
Python bans assignments inside of expressions because it is thought they frequently lead to bugs. For example,
if x = 0 # banned
is too close too
if x == 0 # allowed
avId in self.disconnectedAvIds
will return a boolean value. or
is used to evaluate between two boolean values, so it's correct so far.
self.resultsStr += curStr
is an assignment to a variable. An assignment to a variable is of the form lvalue = rvalue
where lvalue
is a variable you can assign values to and rvalue
is some expression that evaluates to the data type of the lvalue
. This will not return a boolean value, so the expression is illegal.
If you want to change the value if avId is in self.disconnectedAvIds
then use an if statement.
if avId in self.disconnectedAvIds:
self.resultsStr += curStr