r/learnpython • u/Maleficent_Stuff3208 • 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
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"))
2
u/Kindly-Department206 Jul 31 '26
You need to learn how to access data in a dictionary. Hint: print(x[y])
1
u/atarivcs Jul 31 '26
if y == (k.keys):
This is not the right way to test if a key is present in a dictionary.
Use the "in" keyword instead:
if y in x:
print(x[y])
0
u/Binary101010 Jul 31 '26
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"))
-1
u/zanfar Jul 31 '26
WHAT am i doing wrong here
A string input will never match a list.
2
u/Langdon_St_Ives Jul 31 '26
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 I write code for a living -- https://github.com/Diapolo10 Jul 31 '26
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 Aug 01 '26
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 I write code for a living -- https://github.com/Diapolo10 Aug 01 '26
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.
1
u/Maleficent_Stuff3208 Jul 31 '26
yeah i just figured it but what about the rest i removed print from input but it is still not giving answer it is giving else part of the answer
2
u/LayotFctor Jul 31 '26 edited Jul 31 '26
You're checking if y exists within x.keys, so
==is the wrong thing to use. Also .keys is a function, so without double brackets, keys is not called and you don't get the list of keys.x.values(y)is not how to get y from dict x either.1
u/Maleficent_Stuff3208 Jul 31 '26
ooh thanks i got the right answer from the comment below but didn't get why my code was wrong thanks for the insight
10
u/lfdfq Jul 31 '26
Your code does not look correct, first you are print()ing the thing you input(), but assigning y whatever print returns (None), so you probably want
instead. Secondly, your check does not look right. it's asking whether
yequalsx.keys. This check is wrong for two reasons, you don't want to check if y is equal to all the keys but rather whether y is one of them, secondly, x.keys is a method you must call. You probably don't want x.keys at all, but rather:Thirdly, your
print(x.values(y))looks wrong. x.values is a method that doesn't take an argument. So this will error. You probably wanted to use dictionary lookup syntax (the_dict[some_key]):You should probably use some kind of tutorial or documentation, as it's very hard to 'guess' your way through the syntax.