what does can't assign to literal mean in python? -


this question has answer here:

i have been writing programs in python, comes can't assign literal, mean? , causes it? i've searched try , find can't find it.

the object on left-hand side of assignment statement can not literal. a literal string, number, tuple, list, dict, boolean, or none. example, these raise syntaxerror: can't assign literal:

>>> 'foo' = 1 >>> 5 = 1 >>> [1, 2] = 3 

this syntaxerror can happen through indirect assignment:

>>> 'foo' in [1,2,3]: ....    pass syntaxerror: can't assign literal 

in for-loop, python tries assign values 1, 2, 3 literal string 'foo', raises syntaxerror.

the fix, of course, supply variable name such foo, not string, 'foo':

for foo in [1,2,3]:     pass 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -