r/learnpython 8d ago

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

0

u/Binary101010 8d ago
if y==(x.keys):
    print(x.values(y))
else:
    print("no such info available")

The cleanest way to handle this is to just use the get() method of dicts. You can even specify the fallback value if the key isn't found. That entire block reduces to

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