Syntaxerror: can’t assign to Operator

Asked

Viewed 1,180 times

0

I’m using the following code:

Levels[track][level] > UnpaidMaxSkills[track] or self.inventory[track][level] += amount

Only I get the mistake:

Syntaxerror: can’t assign to Operator

What might be going on?

  • The error itself is saying, you cannot use an assignment as an operator.

1 answer

3


I don’t understand exactly what you want to do with this chunk of code, but the error occurs because of a Augmented assignment statement (increased assignment statement?) in an expression.

If I have read the documentation correctly, when there is an assignment of this type, the Python interpreter evaluates to assign what is in the right (expression) and assigns to what is in the left operator (target). The result of this cannot be used in an expression.

In your case, when using the allocation operator +=, Python is trying to assign amount to Levels[track][level] > UnpaidMaxSkills[track] or self.inventory[track][level], but this is not something that can receive an assignment.

I didn’t fully understand the purpose of this code, but the solution is to do the increment outside the expression or other type of verification. Example:

if Levels[track][level] > UnpaidMaxSkills[track]:
    # fazer algo
else:
    self.inventory[track][level] += amount
    if self.inventory[track][level]:
        # fazer algo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.