I’ve made the following dictionary:
client_dict = {'client 1':['ABC', 'EFG'], 'client 2':['MNO','XYZ'], 'client 3':['ZZZ']}
I want to: get input from the user, show the value of the client and if ok keep the current state of the dictionary and if not the user can change the values for the given client. To do this I made the following:
x = client_dict[input('Enter the client name:\n')]
print(x)
y = input('if ok enter y otherwise enter n:\n')
if y =='n':
lst = []
for i in range(len(x)):
x[i] = input('enter the correct header:\n')
lst.append(x[i])
client_dict[x] = lst
else:
pass
Suppose in first input I input client 1 and then enter n meaning I want to change values. Then, the algorithm ask me twice to enter the desired header (as client 1 has two values), for the first header I write hello, and for the second I write world. The line up would be as follow:
Enter the client name:
client 1
['ABC', 'EFG']
if ok enter y otherwise enter n:
n
enter the correct header:
hello
enter the correct header:
world
I can now check my client_dict which is modified to:
{'client 1': ['hello', 'world'],
'client 2': ['MNO', 'XYZ'],
'client 3': ['ZZZ']}
which means the code DOES what I want, but when the process is over in the conditional statement, I also get the following error:
TypeError: unhashable type: 'list'
coming from this : client_dict[x] = lst. So I wonder what am I doing wrong? Despite the fact that the code works, it seems there is some issue when over writing the dictionary?
>Solution :
I changed your code, try this:
client_dict = {'client 1':['ABC', 'EFG'], 'client 2':['MNO','XYZ'], 'client 3':['ZZZ']}
x = input('Enter the client name:\n')
print(client_dict[x])
y = input('if ok enter y otherwise enter n:\n')
if y == 'n':
for i in range(len(client_dict[x])):
client_dict[x][i] = input('enter the correct header:\n')
else:
pass
Sorry, try this now. Edited