Python Code Challenges: Lists ['More Than N' Question]

Hi all,

I am learning Python and enjoying the challenges so far.

I am confused about why I keep getting the incorrect answer for this question. It keeps returning True, when it should return False. I think the issue is that the function is telling the computer to return False if there are more items than n’s in the list, and it can’t find the “n” at all - there’s no 1 in the list! I checked the answer and saw how to create the correct function. I just want to know why mine is wrong. Thanks!

Create a function named more_than_n that has three parameters named lst , item , and n .
The function should return True if item appears in the list more than n times. The function should return False otherwise.

> def more_than_n(lst, item, n):
>   if lst.count(item) > lst.count(n):
>     return True
>   else:
>     return False
> print(more_than_n([2, 3, 4], 2, 1))
> 
> #Uncomment the line below when your function is done
> #print(more_than_n([2, 4, 6, 2, 3, 2, 1, 2], 2, 3))

Hello @sister, welcome to the forums! The problem is here:

Here you are counting the number of times n appears in the list. The question, however, is asking if item appears more than n times. Take the following example:

listvar = [1, 2, 2, 3, 2, 1]
print(more_than_n(listvar, 2, 1))
#This should print True, since the number 2 appears more than once in the list
#listvar

I hope this helps!

Thank you! I get it now.

1 Like