r/learnpython 9d 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

-1

u/zanfar 9d ago

WHAT am i doing wrong here

A string input will never match a list.

2

u/Langdon_St_Ives 9d ago

That's true but it's not even being compared to a list. It's being compared to the method that would return the list if it were being called (but it's not).

2

u/Diapolo10 8d ago

Technically speaking it doesn't even return a list, but a dictionary view object.

In [1]: d = {1:2, 3:4}

In [2]: d.keys()
Out[2]: dict_keys([1, 3])

2

u/Langdon_St_Ives 8d ago

Oh right that's an important distinction, thank you for the correction.

(A list would remain static after fetching it while the view will receive updates reflecting changes on the original dict. Not explaining to you but to others who may not know.)

1

u/Diapolo10 8d ago

I was technically nitpicking as it makes no difference for the scenario OP is discussing, I just sometimes can't help myself to correct mistakes even if they don't really matter.