PyPat848
PyPat848

Reputation: 21

Editing Dictionary via User Input - Python

I am creating a program which requires the user to make changes to the dictionary. I can do these with a normal dictionary, however I was advised to hold my data in 'sub dictionaries' like the one below.

I've tried to see if I can get it working by having it change the values for all of the fields in each entry, but even that doesn't seem to be working. I am quite new to python so please bear with me!

VDatabase = {
    "1200033944833": {
         'MAP' : 'XXXX',
         'CODE'   : '0123',
         'Method': 'R',
         'Code1': 'S093733736',
         'Reg ID'  : '01'
    }

Search = input("Search ACCOUNT:")

tmp_dict = VDatabase.get(Search, None)
print(tmp_dict if tmp_dict else "No ACCOUNT Found. \"{}\"".format(Search))

VDatabase["CODE"] = input("Enter CODE:")

print("Changing CODE...")

I was looking to change the value of CODE to whatever the user Input is.

Unfortunately it doesn't do anything, I can alter a regular Dictionary, so I think it's due to it being a 'sub-dictionary' so how would I access these values?

Upvotes: 1

Views: 1070

Answers (1)

javi_p
javi_p

Reputation: 64

Here in the line,

VDatabase["CODE"] = input("Enter CODE:")

You are trying to change the value of 'CODE' directly in VDatabase but not inside the sub-dictionary that you have searched for.

Search = str(input("Search ACCOUNT:"))
tmp_dict = VDatabase.get(Search, None)
print(tmp_dict if tmp_dict else "No ACCOUNT Found. \"{}\"".format(Search))

VDatabase[Search]["CODE"] = str(input("Enter CODE:"))
print(VDatabase[Search])

or tmp_dict['CODE'] = str(input("Enter CODE:"))

You will see that the main dictionary has changed.

I have changed the input type to str so that the value won't be integer while searching.

Upvotes: 1

Related Questions