How to create a variable through the answer typed by the user in Python?

Asked

Viewed 1,358 times

0

For example, we can use this code to assign the value to a variable:

idade = input('Digite sua idade: ')

What I’d like to know is, if instead of assigning a value a variable is possible to create a variable by naming it with a value typed by the user?

  • 1

    I didn’t quite understand what you mean by "create a variable with a response typed by the user" you can give an example sff

  • What the user writes becomes a variable. For example: if the user writes "John" - then a variable called "John" is created.

  • I think you can try to build something using the getattr. But just out of curiosity: what is the/the advantage/goal of doing this? It is not easier and practical to simply use a dictionary?

  • You want to store the value "John" in the variable, that’s it?

  • The variable with accents is not good practice

  • You can use the command exec to create the variable and assign any value to it, but I’m not sure if that would be good practice from a security point of view

  • 1

    @Math does not. Note only: variable name_da_name = variable value_da_variable --- what the user writes with input code is usually used to assign the right variable value_da_value. What I want is to use the answer typed by the user to create a variable (variable name_da_name).

  • @Luiz Vieira for now my doubt is simply a study

  • 3

    But the variable is just a name to reference a memory position, at runtime the variable name is completely irrelevant, it only matters to the programmer. Unless I’m getting this wrong.

  • 1

    Thank you all for your comments. I considered the question answered with the @Math comment >But the variable is just a name to reference a position

Show 5 more comments

3 answers

3

The variable is just a name to reference an object that occupies a memory position. At runtime the variable name is completely irrelevant since no one will be viewing it at this point.

The variable name is important for the programmer to develop their logic correctly, so there is no reason to change its name at runtime.

3

As colleagues reported this may not be the best way to solve your problem, but in python you can assign a variable with the value of the other yes with the exec.

Remember that this will not change the variable name but create another one from the entered value.

Following example:

foo = "bar" //criando a variável foo com o valor "bar"
exec(foo + " = 'Valor da variavel bar'")
print (bar) // saída: Valor da variavel bar

3

If the idea is to use variable variables you will need to use a dictionary to do this, as mentioned by @Luizvieira. However, it is not the same thing as PHP and I don’t know a Python engine that can do this.

See a small example:

meudic = {}
varnome = "variavelDoGato"
meudic[varnome] = "meow"
print(meudic["variavelDoGato"])

Exit:

Meow

Source:
How do I create a variable number of variables?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.