So i want to define a variable with the index of a current step, and then print that variable. For example I have a loop with 10 steps, and i want to define 10 variable: x1, x2, x3....x10 and then print them out: x1=something, x2=something, ......, x10=something.Does anyone know how to do this?

Thank you

Recommended Answers

All 3 Replies

So i want to define a variable with the index of a current step, and then print that variable. For example I have a loop with 10 steps, and i want to define 10 variable: x1, x2, x3....x10 and then print them out: x1=something, x2=something, ......, x10=something.Does anyone know how to do this?

Thank you

I think you may want to use a list something like this:

x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(10):
    print x[i]

To add data to the list during the loop you could do something like

x = []
for i in range(10):
    x.append(i)
    print x[i]
Member Avatar for masterofpuppets

hi,
you could also use a dictionary if you want to assign values to the variables, e.g.:

d = {}
for i in range( 10 ):
    d[ "x" + str( i ) ] = i

print d

# -> {'x8': 8, 'x9': 9, 'x2': 2, 'x3': 3, 'x0': 0, 'x1': 1, 'x6': 6, 'x7': 7, 'x4': 4, 'x5': 5}

# then you can just use the value from the dictionary like this

print d[ "x5" ]
# -> 5

not sure if this is what you mean but hope it helps anyway :)

Thanks guys, just wat I needed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.