How to create a dictionary whose name corresponds to an entry of the user?

I ask a user to enter his name and I would like that following this action, a dictionary (with the name of the user as variable name) can be created with some data inside.

To explain to me :
if user enters User1 or User2, I would like

user1 = {
   "name" : "User1",
   "matricle" : "Q12E4886"
}

user2 = {
   "name" : "User2",
   "matricle" : "568DF232"
}
1 Like

Ok, so you want to dynamically create variables during the execution of the code. Bad idea, but let me start by providing an answer.

Many scripting languages have a mechanism that allows us to execute code provided in the string format. JavaScript has eval, ruby has eval, Python has exec, etc. So we can get input from the user, prepare code that should be executed in the form of string variable and simply use exec to execute it. Here is an example:

import hashlib

super_important_token = "1234"

user_name = input("Username: ")
user_name_hash = hashlib.md5(user_name.encode('utf-8')).hexdigest()

variable_name = user_name.lower()

exec(f"{variable_name} = {{'user_name': '{user_name}', 'matricle': '{user_name_hash}'}}")

exec(f"print({variable_name})")

print(super_important_token)

Don’t worry about the hashlib thing, I don’t know what matricle should represent so I decided to calculate md5 hash for the user_name.

Execution of this script results in this:

Username: 
=> Factoradic
{'user_name': 'Factoradic', 'matricle': 'e719e9605a9708ca29ba4e9998bc5e73'}
1234

pretty similar to what you need.


Now, let me explain why this solution is problematic.

The most obvious reason - it’s UGLY. I mean, take a look at this line:

exec(f"{variable_name} = {{'user_name': '{user_name}', 'matricle': '{user_name_hash}'}}")

do you really want to write code that looks like that? I hope no :slight_smile:

Maybe not so obvious reason, but not surprising (variable named super_important_token is anything but mysterious) - user of your script can accidently overwrite already defined variables, example:

Username:
=> super_important_token
{'user_name': 'super_important_token', 'matricle': '7f0a09ec0d4cb6389420b6ee27af2c4a'}
{'user_name': 'super_important_token', 'matricle': '7f0a09ec0d4cb6389420b6ee27af2c4a'}

… previous value of super_important_token is gone.

And there are many more reasons why you should not use exac, especially in combination with input, you never want to give your users such power, never.


How to solve this problem without use of exec? You could use a dictionary to store the data, it’s quite simple:

import hashlib

super_important_token = "1234"
user_data = {}

user_name = input("Username: ")
user_name_hash = hashlib.md5(user_name.encode('utf-8')).hexdigest()

variable_name = user_name.lower()

user_data[variable_name] = {
  "user_name": user_name,
  "matricle": user_name_hash
}

print(user_data[variable_name])

print(super_important_token)
1 Like

Thanks. It’s helped me a lot.

1 Like

Awesome! You are very welcome :panda_face: