How to increase variable names / This is a bad idea

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

those.:

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

Can I increase the number of variable names so that I have the number of variables the user enters? Or should I use a completely different method, like adding all new values ​​to a list?

+5
source share
4 answers

, , .

+11

, ?

, list. mapping .

,

. .


", , ?"

, . . , , .

+8

, , , , exec(). , ( len(), , , ..), :

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

exec() , Python. , .

+6

, .

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)

, . , ...:)

+1

All Articles