0

I have a set of variables: x1, x2, x3, x4, x5.

x1 = 10
x2 = 20
x3 = 30
x4 = 40
x5 = 50
number=1
for looper in range(0,4):
    xnumber = xnumber + 10
    number = number + 1

To get: x1 = 20, x2 = 30, etc. Because I got:

"'xnumber' is not defined"

How can I fix this?

0

4 Answers 4

3

Use an array instead, defining x[0] ... x[4] and replacing xnumber with x[number]. Also, you can do without defining number and use the loop index:

x = range(5)
x[0] = 10
x[1] = 2
x[2] = 30
x[3] = 40
x[4] = 50
for i in range(0,5):
    x[i] = x[i] + 10
Sign up to request clarification or add additional context in comments.

Comments

3

Better use a list for this:

x = [10, 2, 30, 40, 50]
for index, value in enumerate(x):
    x[index] = value + 10

or a dict, if you want some 'names' for your values:

x = {'x1': 10, 'x2': 2, 'x3': 30, 'x4': 40, 'x5': 50}
for key, value in x.items():
    x[key] = value + 10

or a class:

class x:
    x1 = 10
    x2 = 2
    x3 = 30
    x4 = 40
    x5 = 50

for index in range(1, 6):
    attr_name = 'x%d' % index
    setattr(x, attr_name, getattr(x, attr_name) + 10)

2 Comments

This is great! Thanks. Additionally, is there any possible way to include one variable into another one? e.g, if x1, and x2, etc, don't have integer values?
Yes, list and dict values, class attributes can be any object, including lists and dicts: x = {'first_key': 1, '2': [1,2,[4,5]], 3: {'a': 7}}
0

Integers aren't mutable. The only way to get what you want is to reassign each variable to a new integer, and you must do that individually to each variable - there's no way to create a loop to do it.

As an example of what I mean, here's what you could do if the values were mutable. I've changed each variable to contain a list instead of an int.

x1=[10]
x2=[20]
x3=[30]
x4=[40]
x5=[50]
vars = [x1, x2, x3, x4, x5]
for looper in range(0,4):
    vars[looper][0] = vars[looper][0] + 10
print x1,x2,x3,x4,x5
[20] [30] [40] [50] [50]

Notice that looper only goes up to 3 so it misses x5.

4 Comments

This answer is a correct answer for a completely different question. :-)
@LennartRegebro, I don't know what you mean. The question was how to update individual variables from within a loop, and I stand by my answer that you can't without using each variable individually. The other answers that suggest replacing the variables with a list are sidestepping the problem.
@LennartRegebro, see my edit. It's not the cleanest solution to the question but it proves my answer wasn't off topic.
OK, I see what you mean. It's not a very good answer, though, using a list or a dict just solved the problem, as per the other answers.
0

Use a dictionary.

>>> d = {'x1':10,'x2':20,'x3':30,'x4':40,'x5':50}
>>> for key in d:
...   d[key] = d[key] + 10
>>> for key in sorted(d):
...   print key, '=', d[key]
...
x1 = 20
x2 = 30
x3 = 40
x4 = 50
x5 = 60

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.