Simple code

Hey guys. Here is the task from one of the first chapters of python3:

here is the solution:
def more_frequent_item(lst, item1, item2):
if lst.count(item1) >= lst.count(item2):
return item1
else:
return item2

And here is my solution:
def more_frequent_item(lst, item1, item2):
item1_c = lst.count(item1)
item2_c = lst.count(item2)
if (item1_c >= item2_c):
return item1_c
else:
return item2_c
As You can see there are almost identical, however my solution always return first value of var item1_c. I’ve checked the value of item1_c and item2_c, both of em are correct. What did I do wrong?

Thank You.

I used your code and found that it worked using the below input.
What input are you using for your code?

lst = [x for x in ‘aaaabbb’]
item1 = input()
item2 = input()
print(more_frequent_item(lst,item1,item2))

Hey, thanks for answer. Here is the picture:

I can see by the output of your code in the screenshot that your code is working. There are 5 "3"s and 4 "2"s. Your program is fine. The problem is the output that you want. You are outputting the amount of the item not the item itself.

In your return statements, you need to remove the “_c” part so that it looks like this:

return item1
else:
return item2

3 Likes

shoot, I did not notice that, Thanks :slight_smile:

2 Likes

No Problem, Hombre :slightly_smiling_face: