Sea
Sea

Reputation: 13

Finding equal values in dictionary

val = dict()
def vote(val,person):
    if person not in val:
        val[person] = 1
    else:
       val[person] = val[person] + 1

def votes(val,person):
    if person in val:
        print val[person]

def result(val):
    ex = sorted(val.items(), key = lambda val:val[1], reverse=True)
    if len(ex) == 0:
        print '***NONE***'
    #Problem below
    elif ex[0] == ex[1]:
        print '***NONE***'
    else:
        print ex[0][0]   
Output: 
>>>vote(val,'Peter')
>>>vote(val,'Peter')
>>>votes(val,'Peter')
2
>>>vote(val,'Lisa')
>>>vote(val,'Lisa')
>>>votes(val,'Lisa')
2
>>>result(val)
Lisa
>>> print val
{'Lisa': 2, 'Peter': 2}  

I want to try to find if 2 keys have the same values, and if they do I want to print "NONE" if that happens. Obviously it doesn't work since it prints "Lisa" instead, any tips on how to do it?

Upvotes: 1

Views: 50

Answers (1)

Naren P
Naren P

Reputation: 214

In your result function need to check the votes in the elif part.

 elif ex[0][1] == ex[1][1]:
    print ('***NONE***')

Upvotes: 1

Related Questions