Python Forum
Naming a variable with the str value and another variable - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Naming a variable with the str value and another variable (/thread-17719.html)



Naming a variable with the str value and another variable - SheeppOSU - Apr-21-2019

I want to create a bunch of variables with just a for loop like this -
x = 0
for i in range(1, 11):
    x += 1
    #name a variable "UserLvl_Check" and I want to put x where the underscore is



RE: Naming a variable with the str value and another variable - Yoriz - Apr-21-2019

you could keep the variables in a dictionary
my_dict = {}
for i in range(1, 11):
    my_dict[f'UserLvl{i}Check'] = i

print(my_dict)
Output:
{'UserLvl1Check': 1, 'UserLvl2Check': 2, 'UserLvl3Check': 3, 'UserLvl4Check': 4, 'UserLvl5Check': 5, 'UserLvl6Check': 6, 'UserLvl7Check': 7, 'UserLvl8Check': 8, 'UserLvl9Check': 9, 'UserLvl10Check': 10}



RE: Naming a variable with the str value and another variable - ichabod801 - Apr-21-2019

Or, since it's just a numeric difference in the game, use a list:

UserLvlCheck = list(range(11))
That gives you a an extra level of zero, but keeps all of the numeric indexes the same.