Ask coding questions

← Back to all posts
How to make an input create a variable
Ferris (7)

I want to be able to create new variables without having to go into the code is there any way to do this

LeonDoesCode (420)

If you are using Python like mentioned in the comments, you can use a dictionary to do this. To create the variables, you can use a function like:

# To store all of your variables
allvars = {}

# Adds a variable name and value of each variable
def addvar(name, val):
  allvars[name] = val

You can then access your variables from the dictionary, like so:

# Loop through all the keys and valuses in the dictionary
for name, val in allvars.items():
  # Print the name fo the variable and it's value

Hope this is what you were looking for.

Giothecoder (135)

Hey guys, isn’t there some weird scoping with var in js that would allow something like this?

AlexanderBirabe (101)

Considering your profile says you code in python, I think you're looking for:

variableName = input("prompt")

The variable 'variableName' gets set to a string of what the user types in (they have to hit enter to submit). You can replace 'prompt' with whatever you want the input prompt to be.

theangryepicbanana (1692)

What language are you using?

Giothecoder (135)

Show us an example, please. I don’t think it is but, just try and show us an example first.