0

I'm working on a python program where it asks the user for input and then creates a variable with the same name as the user output, like this: Input: Hello And then it creates a new variable called Hello. But if you print goodbye, it should create a new variable called goodbye. I tried googling it, but this question is very specific so I can't figure out how to google it well.I started my program like this:

import cs50
input = get_string("Input: )

Then it's supposed to take the input and create a variable with the name [input]. Is there a way I can do this? If you know a way, please tell me. Thanks a lot :)

Lost in code
  • 313
  • 1
  • 4
  • 17
  • Please read this: https://stackoverflow.com/help/how-to-ask, then revisit your question and show what you have tried and what problems you are having. – michjnich Oct 05 '20 at 06:32
  • Does this answer your question? [Creating dynamically named variables from user input](https://stackoverflow.com/questions/11354214/creating-dynamically-named-variables-from-user-input). Bottom line--don't do this. Chances are there are better ways to solve the problem. – DarrylG Oct 05 '20 at 06:36
  • 1
    Don't do this. Don't dynamically create variables. Use a *container*, in this case, a dict maybe – juanpa.arrivillaga Oct 05 '20 at 06:39
  • @juanpa.arrivillaga 's answer is the correct one! – michjnich Oct 05 '20 at 06:42

3 Answers3

2

Potentially you should consider making and using a dictionary, but to do exactly what you want you use the dictionary of the locals (or global) variables in a python script. You can access it by calling the function locals() (globals()). For example:

locals()['Hello'] = 0
locals()['goodbye'] = 'some text'

print(Hello)
print(goodbye)

And the output is:

0
some text

For more info see this post

Jorge Morgado
  • 1,148
  • 7
  • 23
  • 2
    `locals()` will not actually work in a local scope, only in a global scope, where `locals() is globals()` – juanpa.arrivillaga Oct 05 '20 at 06:40
  • 1
    Correct locals() will not work in all cases. – Bharath Oct 05 '20 at 06:41
  • 1
    @Bharath to be precise, it doesn't work *whenever you are in a `local` scope*. Basically, there is no easy way to dynamically modify local variables – juanpa.arrivillaga Oct 05 '20 at 06:43
  • 1
    exactly, that's why i put the two of them, so he/she would know the existence of both options. Maybe i should specified this issue – Jorge Morgado Oct 05 '20 at 06:43
  • 1
    Yes, i agree with you @juanpa.arrivillaga. That is what I wanted to say - perhaps I should have been more elaborate but I saw you already explained it adequately. Thank you again. – Bharath Oct 05 '20 at 06:44
2

Try this

user_input = input("Enter a variable name: ")
globals()[user_input]=25 #variable is dynamically created and is assigned to 25

so if the user inputs hello check the above code using type(hello) and print(hello)

Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
Bharath
  • 406
  • 2
  • 13
-1

Create a dictionary variable in python and use this dictionary to dynamically create variables.

For example, myVars['Hello']='Hi' is the same as Hello='Hi'

Vidyadhar Rao
  • 333
  • 3
  • 10