+ 2
How to search for a key using value in dictionary?
We need to enter a value and print it's key I've done this but not getting the answer. Please tell where I've gone wrong. https://code.sololearn.com/cXUWJIDnvB2u/?ref=app
1 Answer
+ 3
Your logic is correct. But there are some problems with your code.
1. Input returns a string. So when you compare direct input with and int it always gives False ('3' != 3). So you need.to convert input to an integer using int() method
2. Other is the.way that you have positioned else statement.
Below is the corrected version.
d={"a":2,"b":3,"c":4}
n=int(input("enter value"))
for i in d:
if d[i]==n:
print(n,"present at",i)
else:
print("no such value")