Home > database >  Error in getting values from python dictionary
Error in getting values from python dictionary

Time:01-03

I've created a python dictionary

user = input('enter a letter: ')
d = {'a': '1', 'b': '2', 'c':'3'}
print(d.get(d))

I run that and enterned

a 

But I always get an error like this

er a letter: a
Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
    start(fakepyfile,mainpyfile)
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
    exec(open(mainpyfile).read(),  __main__.__dict__)
  File "<string>", line 3, in <module>
TypeError: unhashable type: 'dict'

[Program finished]

Help me to slove!

CodePudding user response:

user = input("enter a letter: ")
d = {"a": "1", "b": "2", "c": "3"}
print(d.get(user)) #user instead of d

CodePudding user response:

You have to use the variable with user's input:

user = input('enter a letter: ')
d = {'a': '1', 'b': '2', 'c':'3'}
print(d.get(user))

CodePudding user response:

You can use d.get(user) or use d[user] instead

user = input('enter a letter: ')
d = {'a': '1', 'b': '2', 'c':'3'}
print(d.get(user))

CodePudding user response:

You are searching for dictionary in dictionary. Use this:

user = input('enter a letter: ')
d = {'a': '1', 'b': '2', 'c':'3'}
print(d.get(user))
  • Related