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

wjandrea
  • 28,235
  • 9
  • 60
  • 81
TheFoxx
  • 1,583
  • 6
  • 21
  • 28

6 Answers6

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'
Anupam
  • 14,950
  • 19
  • 67
  • 94
21

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

cheeken
  • 33,663
  • 4
  • 35
  • 42
  • That gives me a type error TypeError: Can't convert 'float' object to str implicitly Any ideas? – TheFoxx Jan 21 '12 at 21:36
  • 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. – cheeken Jan 21 '12 at 21:38
1

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

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • I just tried that and its telling me TypeError: Can't convert 'float' object to str implicitly – TheFoxx Jan 21 '12 at 21:34
  • HINT: You can't use `distribution` in that sense in Python's `for` statement. Perhaps a `range` of your distribution would be suitable...? – Makoto Jan 21 '12 at 21:37
1

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

kindall
  • 178,883
  • 35
  • 278
  • 309
  • 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.. – TheFoxx Jan 21 '12 at 21:37
-1

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.

Marcin
  • 48,559
  • 18
  • 128
  • 201
  • 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 Jan 21 '12 at 21:33
  • @user1048244: It's not relevant what it's for - as written it's not syntactically valid. – Marcin Jan 21 '12 at 22:26
-8

in python only work

a=4
b=3

i=a+b

which i is new operator

Liam
  • 27,717
  • 28
  • 128
  • 190