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?
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
– Miguel
What the user writes becomes a variable. For example: if the user writes "John" - then a variable called "John" is created.
– Dérick Ramos
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?– Luiz Vieira
You want to store the value "John" in the variable, that’s it?
– Math
The variable with accents is not good practice
– Miguel
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– Gomiero
@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).
– Dérick Ramos
@Luiz Vieira for now my doubt is simply a study
– Dérick Ramos
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.
– Math
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
– Dérick Ramos