Why does my function present the wrong counts?

Can anyone tell me why the below code is returning the wrong variable?

I think it has to do with using an IF and then an ELIF statement but I can’t see why that would be:

#Write your function here
def more_frequent_item(lst,item1,item2):
  if lst.count('item1') >= lst.count('item2'):
    return item1 
  elif lst.count('item2') > lst.count('item1'):
    return item2 
#Uncomment the line below when your function is done
print(more_frequent_item([2, 3, 3, 2, 3, 2, 3, 2, 3], 2, 3))

I don’t think you need the quotes around item1 and item2 as part of .count(). I think what your function is doing is looking for the strings ‘item1’ and ‘item2’ in the list. It will get 0 for each of these so will always return item1.

1 Like