August 3rd, 2005, 12:41 PM
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!!
August 3rd, 2005, 01:23 PM
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
this would print out
listOfVeribles = 
for i in range(10):
for i in range(10):
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
August 3rd, 2005, 03:31 PM
The declaration of the variable:
Now you can assign a value to each variable anywhere in the program.
August 3rd, 2005, 03:57 PM
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:
>>>foo = "null"
August 3rd, 2005, 04:31 PM
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.
August 3rd, 2005, 06:26 PM
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.