Python Forum

Full Version: Looping through variables and setting them
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm not new to programming, however I am new to Python.

To learn python, I've decided to make an AI for tic tac toe (or knots and crosses).

I've come pretty far, using different techniques of programming. I'm currently a bit stuck.

I have the following variables:

(pos1, pos2, pos3 ... pos7, pos8 ,pos9):
To reduce size, instead of setting them individually, I want to itterate through the variables and setting to a certain value, like the following:

for i in range(1:9):
    eval("pos" + str(i) = i
Result:

pos1 = 1
pos2 = 2
pos3 = 3
.
.
.
pos7 = 7
pos8 = 8
pos9 = 9
However this does not work because "I can't assign eval to function call."

I've tried to Google the answer, but it only showed how I can use the eval function on the other side of the equal sign.


Please help,
W.
If you going to have pos1 through pos9. Why not use a list or at least a dict.
List start at 0.
pylist = [1,2,3,4,5,6,7,8,9]
for l in pylist:
    l += 1
print(pylist)
I want to use as much techniques as possible. That's the only way I can learn to program even better.
Learning techniques is good. But programming should always be kept simple as possible. The KISS philosophy.

I wouldn't use this in code.
exec('pos{0} + i'.format(i))
(Dec-17-2017, 01:33 PM)ColdDeath Wrote: [ -> ]I want to use as much techniques as possible. That's the only way I can learn to program even better.

I concur with the Windspar. You should try keeps things as simple as possible. Particular for what your trying to do list should suffice perfectly., The problem with using function like eval and exec is a potential threat to security. It allows to create a security hole to access the underlying system. As a general rule try avoiding using eval or exec :)
eval() is not needed and also is dangerous. Why do you want to create a bunch of variables separately? Why don't create just a list? How would you use them? Typing all by hand when you can just loop them all.
Using eval and exec is for sure not the way to go.
Think of what happen when do pos1 = 1 Think
>>> pos1 = 1
>>> pos1
1
Internally  is python storing this a dictionary.
>>> globals()
{'__name__': '__main__', 'pos': 1,}
>>> globals()['pos1']
1
We should not do the tricks and using the globals() dictionary.
Making a normal visible dictionary is the way to go.
>>> d = {}
>>> for i in range(1,9):
...     d[f'pos{i}'] = i
...
    
>>> d
{'pos1': 1,
 'pos2': 2,
 'pos3': 3,
 'pos4': 4,
 'pos5': 5,
 'pos6': 6,
 'pos7': 7,
 'pos8': 8}

>>> d['pos1']
1
Cool! Thank you all for the reaction!

That made matters worse for me, so I know I'm learning, cool!

I've used the eval before to loop through lists and inserting pieces of string. However, from my understanding:
eval.type = "evil"
eval.usage = "never ever"
I now have to figure out how to use lists and dictionaries as an multi-dimensional array.