16

In Python, if I were to have a user input the number X, and then the program enters a for loop in which the user inputs X values, is there a way/is it a bad idea to have variable names automatically increment?

ie:

user inputs '6'  
value_1 = ...  
value_2 = ...  
value_3 = ...  
value_4 = ...  
value_5 = ...  
value_6 = ...

Can I make variable names increment like that so that I can have the number of variables that the user inputs? Or should I be using a completely different method such as appending all the new values onto a list?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
tom
  • 161
  • 1
  • 1
  • 3
  • 9
    Use a list instead. – Alexander Gessler Mar 21 '10 at 19:47
  • 3
    Or dictionary, keyed by the index value. – S.Lott Mar 21 '10 at 19:57
  • 1
    okay I understand that using a list/dictionary is better. but just out of complete curiosity, is these a way to do it in the asked manner? – tom Mar 21 '10 at 20:33
  • There's little point doing it the way you describe. Variable names are for you, the programmer, to be able to reference an object that you know exists in some form. But if you don't know that a value exists at all (eg. the user might type 1, so there would be no 'value_2' created) then how can you adequately use it? – Kylotan Mar 22 '10 at 11:44

4 Answers4

18

You should append all the values to a list, that allows you to easily iterate through the values later as well as not littering your namespace with useless variables and magic.

knutin
  • 5,033
  • 19
  • 26
12

While using a list or dictionary is of course the right approach here, it is possible to create named variables, using exec(). You'll miss out on all the convenient operations that lists and dictionaries provide (think of len(), inserting, removing, sorting, and so on), but it's possible:

count = raw_input('Number of variables:')
for i in xrange(count):
    exec('var_' + str(i) + ' = ' + str(i))

exec() accepts and executes strings that contain valid Python expressions. So the above piece of code is generating and executing code on the fly.

Pieter Witvoet
  • 2,773
  • 1
  • 22
  • 33
  • My approach is.., new_var = `eval('old_var' + str(count))` – Shirjeel Ahmed Khan Apr 26 '19 at 05:42
  • @ShirjeelAhmedKhan: that doesn't do what the OP was asking about: you can't dynamically _create_ variables with `eval`. Again, using a list or dictionary is a much better solution. `exec` and `eval` can (easily) lead to security problems due to code injection. Consider what happens when `count = -1`, or worse, `count = '+ os.rmdir("...")'`. – Pieter Witvoet Apr 26 '19 at 07:57
9

should I be using a completely different method such as appending all the new values onto a list?

Yes, you can use a list. You can also use a mapping for this.

make variable names increment like that

No. That's a horrifyingly bad idea.


"but just out of complete curiosity, is these a way to do it in the asked manner?"

Yes, but it's clearly a horrifyingly bad idea. So, there's no point in showing it. It's a terrible, terrible thing to even attempt.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
0

Maybe this will help you out.

filenamee = 'Value_'
counter1 = 1
counter2 = 2
list1 = []
list2 = range(30)    #random number, given len needed
for x in list2:
    counter1 = str(counter1)
    full_name = (filenamee+counter1)
    list1.append(full_name)
    counter1 = counter2
    counter2+=1

for x in list1:
    print(x)

If you're looking to make an incremental variable, this works for me. I've got no clue whether this is clean programming or not... :)

Take care