Python Forum

Full Version: Naming a variable with the str value and another variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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}
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.