Python Forum
Why this reverse lookup function not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why this reverse lookup function not working
#1
Please, can someone help me explain why this reverse lookup is not working. If I input any of the values in the dictionary, it says not found. Baffled.
mydict = {'a':2, 'b':4, 'c': 5, 'u':98}
#do reverse lookup
v = input('enter a key >')
def reverse_lookup(d, v):
    for keys in d :
        if d[keys] == v :
            print('found')
            return keys
        else :
            print('not found')
            
print(reverse_lookup(mydict, v))
Thanks
Reply
#2
https://stackoverflow.com/questions/8214...ary-python
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
(May-31-2020, 05:30 AM)pyzyx3qwerty Wrote: https://stackoverflow.com/questions/8214...ary-python

Thanks. But the reference is very old. I am using python 3. The reference is for python 2. I tried out using mydict.values() and received a dict_values types and not a list which was the implementation in python 2. I don't know how to traverse a dict_values type. If you can show me, then I would be happy.
Reply
#4
input returns str and the values in the dict are int, so the user input and [any] value from the dict will never be equal.
Do you really want to print 'not found' for each not equal value? Also there are better ways to iterate over dict:
for key, value in some_dict.items():
    # do something with key and value
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(May-31-2020, 05:48 AM)buran Wrote: input returns str and the values in the dict are int, so the user input and [any] value from the dict will never be equal.
Do you really want to print 'not found' for each not equal value? Also there are better ways to iterate over dict:
for key, value in dict.items():
    # do something with key and value
Thanks. This solution is more intuitive and it worked.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reverse Function in Python Voraman 7 3,285 Feb-13-2021, 07:21 PM
Last Post: Voraman
  GUI and function not working together albry 2 2,571 Jan-15-2019, 07:32 AM
Last Post: albry
  Function not working as intended I think? TimeForged 2 3,049 Mar-11-2018, 09:05 AM
Last Post: buran
  [split] Function not working as intended mihshyahoocom 1 2,102 Mar-11-2018, 09:04 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020