Python Forum
Naming a variable with the str value and another variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Naming a variable with the str value and another variable
#1
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
Reply
#2
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}
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I trying to automate the Variable Logon button using the python code but I couldn't surendrasamudrala 0 257 Mar-07-2025, 05:02 AM
Last Post: surendrasamudrala
  not able to call the variable inside the if/elif function mareeswaran 3 508 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  creating arbitrary local variable names Skaperen 9 1,760 Sep-07-2024, 12:12 AM
Last Post: Skaperen
  Variable Substitution call keys Bobbee 15 2,633 Aug-28-2024, 01:52 PM
Last Post: Bobbee
  how solve: local variable referenced before assignment ? trix 5 1,685 Jun-15-2024, 07:15 PM
Last Post: trix
  Variable being erased inside of if statement deusablutum 8 2,035 Jun-15-2024, 07:00 PM
Last Post: ndc85430
  Cant contain variable in regex robertkwild 3 1,049 Jun-12-2024, 11:50 AM
Last Post: deanhystad
  is this a valid variable name? Skaperen 6 1,634 Jun-05-2024, 10:13 PM
Last Post: Skaperen
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 1,390 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Commas issue in variable ddahlman 6 1,707 Apr-05-2024, 03:45 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020