Nathum
Nathum

Reputation: 3

Python can't assign to operator

I keep getting SyntaxError:

can't assign to operator when trying to assign a ID

Working Code

canvas = self.matrix.CreateFrameCanvas()
graphics.DrawText(canvas, font, LEFT, TOP, setcolor, MESSAGE)
self.matrix.SwapOnVSync(canvas)

What I am trying to achieve

ID = "4"
canvas+ID = self.matrix.CreateFrameCanvas()
graphics.DrawText(canvas+ID, font, LEFT, TOP, setcolor, MESSAGE)
self.matrix.SwapOnVSync(canvas+ID)

Error:

canvas+ID = self.matrix.CreateFrameCanvas()
SyntaxError: can't assign to operator

Upvotes: 0

Views: 323

Answers (2)

user8105524
user8105524

Reputation:

If you want to programatically assign multiple variables like canvas4 and/or canvas5 and so on, you should probably be using a list or dictionary, not multiple variables.

How about something like:

canvases = {}
ID = 4

canvases[ID] = self.matrix.CreateFrameCanvas()
graphics.DrawText(canvases[ID], font, LEFT, TOP, setcolor, MESSAGE)
self.matrix.SwapOnVSync(canvases[ID])

Upvotes: 1

vlizana
vlizana

Reputation: 3252

The operator + is used to concatenate strings, not variable names. although what you want can be done by using eval('canvas'+ID+'= self.matrix.CreateFrameCanvas()') it is a terrible practice, use a list instead.

Upvotes: 1

Related Questions