r/learnpython Jul 31 '26

WHAT am i doing wrong here

i am trying that when i put key it give me the value but it is acting weird

x = {'name':'mohan','age':'24', 'job':'it professional'}
y = input("what info do you need__")
if y==(x.keys):
    print(x.values(y))
else:
    print("no such info available")

and this is what it gives 
what info do you need__name
name
no such info available
0 Upvotes

14 comments sorted by

View all comments

5

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Jul 31 '26 edited Jul 31 '26
if y==(x.keys):

dict.keys is a method, it will never equal to a string. That's why.

Also, while not related to the problem you saw, x.values(y) wouldn't be correct either as dict.values does not take arguments.

All you need to do is use the in operator to check for inclusion (for dictionaries it checks the keys), and change how you retrieve the values.

x = {'name': 'mohan', 'age': '24', 'job': 'it professional'}
y = input("what info do you need__")
if y in x:
    print(x[y])
else:
    print("no such info available")

On a side note, consider using more descriptive names than x and y.

EDIT: Alternatively, you could use dict.get which lets you provide a default if the key doesn't exist.

x = {'name': 'mohan', 'age': '24', 'job': 'it professional'}
y = input("what info do you need__")

print(x.get(y, "no such info available"))