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

6 Answers 6

52

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'

(OP does not have a hyphen in the variable but putting this here since this is a popular result on searching with that error message)

Sign up to request clarification or add additional context in comments.

2 Comments

There is no variable name with a hyphen in the question.
@mkrieger1 this answer was edited (and hence seemed out of place). I had earlier mentioned that "In case it helps someone, if your variables have hyphens in them, you may see this error.." since this SO post is a popular result for that error message
26

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

2 Comments

That gives me a type error TypeError: Can't convert 'float' object to str implicitly Any ideas?
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.
2

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

1 Comment

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

Instead of

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

you should use

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

(The other syntax issue - int is not iterable - will be your exercise to figure out.)

2 Comments

I just tried that and its telling me TypeError: Can't convert 'float' object to str implicitly
HINT: You can't use distribution in that sense in Python's for statement. Perhaps a range of your distribution would be suitable...?
-1

Make sure that your variable doesn't include a space. If you need to separate, use an underscore (_).

1 Comment

They did make sure and still they got the error.
-2

What do you think this is supposed to be: ((t[1])/length) * t[1] += string

Python can't parse this, it's a syntax error.

2 Comments

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
@user1048244: It's not relevant what it's for - as written it's not syntactically valid.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.