-1

I have a large amount of variables within a class like this:

class Variables:
  num1 = "Text 1"

  num2 = "Text 2"

  num3 = "Text 3"

and so and so forth. I need to assign each variable a specific number from range (1,30) so that within the class, each variable is represented by a different integer value. Any help would be greatly appreciated. Thank You!

  • Possible duplicate http://stackoverflow.com/questions/3996904/generate-random-integers-between-0-and-9 – ᴀʀᴍᴀɴ Jan 10 '16 at 00:05
  • 2
    Python does not use a `var` declaration! – Hugh Bothwell Jan 10 '16 at 00:07
  • What are you trying to accomplish by doing this? – Hugh Bothwell Jan 10 '16 at 00:08
  • Later in the program, I want to be able to get user input in order to call a variable. Its mainly for a game I'm building based on random functions and user input. –  Jan 10 '16 at 00:09
  • Sorry I'm new to python, and have some minor experience in JS. Any help is greatly appreciated –  Jan 10 '16 at 00:11
  • As you defined these variables are `static class members` do you realy want this kind of vars? – ᴀʀᴍᴀɴ Jan 10 '16 at 00:11
  • 3
    @Arthur: I think you would be better off using a dict or list instead of lots of variables! – Hugh Bothwell Jan 10 '16 at 00:14
  • @Hugh Bothwell Okay, thanks. I'll look into using dict given that I have a long series –  Jan 10 '16 at 00:17
  • I was thinking the same thing! See my answer. If you can give any more context on exactly why you're doing this, it will make it easier to show you something more useful! – Tom Dalton Jan 10 '16 at 00:19
  • As Hugh Bothwell said, mapping values to names like this is _exactly_ the kind of thing a `dict` is for. As a bonus, a `dict` can use integers as its keys, so you wouldn't have to glue strings and stringified integers together into names like `var27`. – Kevin J. Chase Jan 10 '16 at 00:35

3 Answers3

2

You have a lot of data in your class that looks like 'name' (at the moment you're using a variable num1, num2 etc for the name) and each 'name' maps to a 'value' (at the moment your values look like "Text 1", "Text 2" etc.

You might find it easier to use a dictionary for this data, rather than trying to define each name:value as a separate variable = value.

For example, you'd end up with something more like:

class Variables(object):
    numbers = {
        "num1": "Text 1",
        "num2": "Text 2",
        "num3": "Text 3",
    }

Now it becomes much more straightforward to procedurally generate (a fancy way of saying automatically) that dictionary:

class Variables(object):
    numbers = {
        "num{}".format(i): "Text {}".format(i)
        for i in range(100)
    }

Now if you want them to be random numbers:

import random

class Variables(object):
    numbers = {
        "num{}".format(i): "Text {}".format(random.randrange(100))
        for i in range(100)
    }

v = Variables()
print v.numbers["num50"]
Tom Dalton
  • 6,122
  • 24
  • 35
  • thanks so much for the feedback. I ran the code in the Python IDLE and adjusted it a little bit to fit my parameters and it worked great. Thanks! –  Jan 10 '16 at 00:21
1

You mean like this?

class Variables():
    def __init__(self):
        for i in range(1, 31):
            setattr(self, 'num{}'.format(i), 'Text {}'.format(i))

v = Variables()

print v.num1 # Text 1
print v.num30 # Text 30
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

First you create your variables and add them to a dictionary :

dic = {num1:"Text 1", num2 : "text2",..}

next create a Variables obejct:

  a = Variables()   

then call set_dic class method:

  a.set_dic(dic)

its assgin each variable a random integer between 1,30

from random import randint  
class Variables:
      def __init__(self):
            self.dic = {}
      def set_dic(self, dic):
            self.dic =  dic
            for x in dic:
                  dic[x] = randint(1,30)
ᴀʀᴍᴀɴ
  • 4,443
  • 8
  • 37
  • 57