How to assign a name to a dictionary based on input from a user

Viewed 151

How do we assign a dictionary a name that is taken from input from the user and save that dictionary to a txt file so that we can search for it by its name and print output to the user? I am currently here:

Any ideas how?

import sys
import pathlib
'''Arg-V's should be in following order <app.py> <action> <nick_name> <name> <phone> <email>'''

if str(sys.argv[1]).lower == 'add':
    current = {'Name': sys.argv[3], 'Phone Number': sys.argv[4], 'Email': sys.argv[5]} 
    with open('contacts.txt', 'w') as f:
        f.write(current)
1 Answers

As per Naming Lists Using User Input :

An indirect answer is that, as several other users have told you, you don't want to let the user choose your variable names, you want to be using a dictionary of lists, so that you have all the lists that users have created together in one place.

import json

name = input('name/s of dictionary/ies : ')


names = {}

name = name.split()

print(name)

for i in name:
    names[i]={}

print(names)

for i in names:
    print(i,'--------->', names[i])

for i in names:
    names[i] = '1'*len(i)
for i in names:
    with open(i+'.txt', 'w+') as file:
        file.write('prova : '+json.dumps(names[i]))
Related