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?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
rollorox202
  • 41
  • 1
  • 1
  • 4

4 Answers4

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
Eduardo Ivanec
  • 11,668
  • 2
  • 39
  • 42
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)
warvariuc
  • 57,116
  • 41
  • 173
  • 227
  • 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? – rollorox202 Mar 03 '12 at 20:49
  • 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}}` – warvariuc Mar 04 '12 at 07:01
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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 1
    This answer is a correct answer for a completely different question. :-) – Lennart Regebro Mar 02 '12 at 19:54
  • @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. – Mark Ransom Mar 02 '12 at 19:59
  • @LennartRegebro, see my edit. It's not the cleanest solution to the question but it proves my answer wasn't off topic. – Mark Ransom Mar 02 '12 at 20:15
  • 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. – Lennart Regebro Mar 03 '12 at 11:48
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
James Robinson
  • 822
  • 6
  • 13