0

I have a string called "sakla" where I want to add variable at the end of it and set it to 1 but getting SyntaxError: Can't assign to operator with the below code:

i=0
string = "sakla"
for i in range(9):
   string + str(i) =1
   i= i+1

My desired outout as follows:

sakla0=1
sakla1=1
sakla2=1
etc...
sakla9=1

Where I am doing wrong ? I need to set to 1 because at some other place I am setting to zero which I am using to hide some colomns at my django template. When I am writing each one by one,my code is working but it doesn't look good.So I need help for the improvement :)

EDİT: Maybe I couldn't express myself while asking the question but the questions that you referenced as duplicate it's not related to what I am after. I just need to set some strings to 1 while doing that I need to increase these string names one by one. You might ask why you want this. The reason is that I am hiding some cells within the django template. So if the value is lets say sakla = 0 then the template is hidden if not it's not hidden.So I only need to set to 1 some string names where it's not working within the django the examples that I found,so I asked this question.

Here is my template codes:
{%if sakla == 0%}
<div style="display:none">
    {% elif sakla == 1 %}
    <div>
        {% endif %}
ahmet
  • 256
  • 2
  • 17
  • Were you going to concatenate the rest of it? – Ignacio Vazquez-Abrams Jul 28 '18 at 19:01
  • There is no `print` in your code. Why are you expecting any output at all? – melpomene Jul 28 '18 at 19:01
  • 1
    Oh, is this question about "variable variables"? If so, the answer is to use an array or a list instead of a bunch of variables with similar names. I.e. use `sakla[i]` instead of trying to dynamically access variables by name at runtime. – melpomene Jul 28 '18 at 19:02
  • 1
    This might be helpful https://stackoverflow.com/questions/4010840/generating-variable-names-on-fly-in-python – Ironlors Jul 28 '18 at 19:04
  • You really _really_ don't want to do that! Although it's _possible_, it's almost always a very bad idea. Instead, use a list or a dict. See the linked questions for explanations and alternatives. – PM 2Ring Jul 28 '18 at 19:10
  • PM 2Ring you set the question duplicate but it's nothing to do with the question that you referenced. My questions intention is totally different and getting error while doing.I also checked earlier those all questions. – ahmet Jul 28 '18 at 19:13
  • I found the answer and nothing to do with the duplicate question marked here for info for the people who might need it for i in range(10): x = "sakla" + str(i) globals()[x] = 1 – ahmet Jul 29 '18 at 19:53

0 Answers0