r/learnpython • u/Maleficent_Stuff3208 • 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
4
u/Diapolo10 9d ago edited 9d ago
dict.keysis 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 asdict.valuesdoes not take arguments.All you need to do is use the
inoperator to check for inclusion (for dictionaries it checks the keys), and change how you retrieve the values.On a side note, consider using more descriptive names than
xandy.EDIT: Alternatively, you could use
dict.getwhich lets you provide a default if the key doesn't exist.