Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: cannot assign to operator

def RandomString (length,distribution):
    string = ""
    for t in distribution:
        ((t[1])/length) * t[1] += string
    return shuffle (string)

This returns a syntax error as described in the title. In this example, distribution is a list of tuples, with each tuple containing a letter, and its distribution, with all the distributions from the list adding up to 100, for example:

[("a",50),("b",20),("c",30)] 

And length is the length of the string that you want.

like image 772
TheFoxx Avatar asked Jan 21 '12 21:01

TheFoxx


People also ask

How do you fix SyntaxError can't assign to function call?

The error is caused because we are trying to assign a value to a function call. To solve the error, make sure to specify the function call on the right-hand side of the assignment. Copied! Make sure the function returns what you expect, because functions that don't explicitly return a value return None .

How do you fix SyntaxError Cannot assign to literal?

The “SyntaxError: can't assign to literal” error occurs when you try to assign a value to a literal value such as a boolean, a string, a list, or a number. To solve this error, ensure your variable names are names rather than values.

What does Cannot assign to expression mean in Python?

The Python "SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?" occurs when we have an expression on the left-hand side of an assignment. To solve the error, specify the variable name on the left and the expression on the right-hand side.

Can you assign an operator to a variable in Python?

Assignment operators are used in Python to assign values to variables. a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a on the left. There are various compound operators in Python like a += 5 that adds to the variable and later assigns the same.


2 Answers

In case it helps someone, if your variables have hyphens in them, you may see this error since hyphens are not allowed in variable names in Python and are used as subtraction operators.

Example:

my-variable = 5   # would result in 'SyntaxError: can't assign to operator'
like image 70
Anupam Avatar answered Sep 22 '22 12:09

Anupam


Python is upset because you are attempting to assign a value to something that can't be assigned a value.

((t[1])/length) * t[1] += string

When you use an assignment operator, you assign the value of what is on the right to the variable or element on the left. In your case, there is no variable or element on the left, but instead an interpreted value: you are trying to assign a value to something that isn't a "container".

Based on what you've written, you're just misunderstanding how this operator works. Just switch your operands, like so.

string += str(((t[1])/length) * t[1])

Note that I've wrapped the assigned value in str in order to convert it into a str so that it is compatible with the string variable it is being assigned to. (Numbers and strings can't be added together.)

like image 39
cheeken Avatar answered Sep 18 '22 12:09

cheeken