#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    12
    Rep Power
    0

    Creating a variable and assigning it a value


    Hi folks --

    Have searched a while for answer to this, but have had no luck. Am pretty sure the answer is simple

    Within a loop, I want to create a variable name and I want to be able to call this variable name later in the program and assign it a numeric value.

    For example, I would like to (a) create var_1, var_2 -> var_n in a loop (var_1 being created in the first iteration of the loop, var_2 being created in the 2nd iteration, var_n being created in the nth iteration, etc...), (b) call these variables by name later in the program so I can assign them a numeric value.

    Any help appreciated!!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    well what your asking for isn't possible for a number of reasons. FIrst Python is dynamicly typed, that means that you can assain somethign to a verible and if it isn' already there it will automaticly be created. Comared to say c or java which force you to declare that you want to create a verible. The second reason is that you cannot dynamicly assign names to veribles in python, because of how the syntax is, there actually isn't very meny languages that allow you to dynamicly name a verible, since in reality there is no reason for it. Much better ways to do things like i will show you.

    The way to do the type of thing your asking for, is use an array or list as they are called with python. Here I will give you a little code of its use

    Code:
    listOfVeribles = []
    for i in range(10):
         listOfVeribles.append(i)
    
    for i in range(10):
         print listOfVeribles[i]
    this would print out
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9

    Now lists are very powerfull tools, you are actually aloting multible things to the same name, but ou are able to call each part using a numical value. There is also things called dictionarys (or hashes in other languages) which allow you to call things with anything as the key such as strings or numbers.

    for more information on lists you can use pythons documentation (http://docs.python.org/tut/tut.html)
    for lists you can get the information at http://docs.python.org/tut/node5.htm...00000000000000
    and for some help with advanced things with strings you can go here: http://docs.python.org/tut/node7.htm...00000000000000

    good luck!
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    The declaration of the variable:
    Code:
    global var_1
    global var_2
    Now you can assign a value to each variable anywhere in the program.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    that does not decale a verible, that makes a verible global. Because if the verible has been used before then it just turns it to global, also since python is dynamic it means that global is actually assign something to that verible. So since it isn't in exsitance yet python will declear it then send it to global. so that is delcaring a verible as much as doing this is:

    Code:
    >>>foo = "null"
    >>>
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    He wanted to use his variable anywhere in the program, thus declaring at as global should be the correct way to go. Now, at the proper time, when he needs to give his variables a value, he can do so anywhere in the program.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    his question was about dynamicly naming these veribles so they can be used for each loop later in the program. If they are decleared at the top of the application then it can be used else where in the app except within things like functions or classes. But your right if it wants to be used within a function or class then it needs to be globaled.

IMN logo majestic logo threadwatch logo seochat tools logo