Help with lists function

So in this exercise we need to create a function named more_frequent_item that has three parameters named lst , item1 , and item2 .
Return either item1 or item2 depending on which item appears more often in lst .
If the two items appear the same number of times, return item1 .

and this is what i get so far:

def more_frequent_item(lst, item1, item2):
  total1 = lst.count(item1)
  total2 = lst.count(item2)
  if total1 == total2:
print(more_frequent_item([2, 3, 3, 2, 3, 2, 3, 2, 3], 2, 3))

but is giving me Syntax Error, and i don’t kow why. I’m a novice programmer, so i think this is simple, but i can’t get it. Sorry and thank you.

First off, welcome to the forums, hope to see you around more!

Secondly, based on the code block you have posted, it looks like your else block is on the wrong indent level. The else should be in line with the if otherwise python will just see a standalone else and get confused.

Also worth mentioning that you are printing the items depending on which appears more, however the exercise asks you to return the item. Just something to watch out for as that’ll flag up an error not in the code, but in codecademy.


Holly! Yeah it was that! I tried so many things, and it never worked. Didn’t knew it must be inline with if. Thank You so much.
Just changed ‘print’ to ‘return’ and it worked.
I’m so gald. Thank You.

1 Like