Suppose user enters hello
I want to create empty dictionary hello = {}
which will be later used as required.
Suppose user enters hello
I want to create empty dictionary hello = {}
which will be later used as required.
If you want to create a dictionary whose name is input by the user, you can use something like this:
while True:
dictName = raw_input('Name: ') # use input on Python 3
if not dictName:
break
globals()[dictName] = {}
print test1
print test2
The code above will ask for a name and inject a new dict into globals
, so you can reference that later in the code (ie. print it). It doesn't use eval
, which is unsafe.
Example usage:
Name: test1
Name: test2
Name:
{}
{}