SyntaxError: cannot assign to operator

222,048

Solution 1

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.)

Solution 2

Make sure the variable does not have a hyphen (-).

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'

Solution 3

Well, as the error says, you have an expression (((t[1])/length) * t[1]) on the left side of the assignment, rather than a variable name. You have that expression, and then you tell Python to add string to it (which is always "") and assign it to... where? ((t[1])/length) * t[1] isn't a variable name, so you can't store the result into it.

Did you mean string += ((t[1])/length) * t[1]? That would make more sense. Of course, you're still trying to add a number to a string, or multiply by a string... one of those t[1]s should probably be a t[0].

Share:
222,048
TheFoxx
Author by

TheFoxx

Updated on July 09, 2022

Comments

  • TheFoxx
    TheFoxx over 1 year
    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.

  • TheFoxx
    TheFoxx almost 12 years
    Well actually its meant to be this, I just relised ((t[1]/10)/length) * t[1] Basically its taking the destribution for each letter, figuring out how many times that letter needs to be in random string that its making, and then adding that many letters to the empty string, named string
  • TheFoxx
    TheFoxx almost 12 years
    I just tried that and its telling me TypeError: Can't convert 'float' object to str implicitly
  • TheFoxx
    TheFoxx almost 12 years
    That gives me a type error TypeError: Can't convert 'float' object to str implicitly Any ideas?
  • Makoto
    Makoto almost 12 years
    HINT: You can't use distribution in that sense in Python's for statement. Perhaps a range of your distribution would be suitable...?
  • TheFoxx
    TheFoxx almost 12 years
    Yes it should t[0], my code isn't normally this full of errors, ive just retyped it about 10 times trying to figure this our..
  • cheeken
    cheeken almost 12 years
    Yes. But it all depends on what you want string to look like. Please elaborate on what the value of string should be based on the example input you provided. I've amended my answer with a guess as to what you are looking for.
  • Marcin
    Marcin almost 12 years
    @user1048244: It's not relevant what it's for - as written it's not syntactically valid.