How am I supposed to define 'n' in this scenario for

How am I supposed to define ‘n’ in the below scenario? What is ‘n’? Is it the number of items in the list or is it a specific number?

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.

Here is what I have so far:

#Write your function here

def more_than_n(lst, item, n):
  length_list = len(lst)
  if item > length_list:
    return True
  else:
    False


#Uncomment the line below when your function is done
print(more_than_n([2, 4, 6, 2, 3, 2, 1, 2], 2, 3))

n is reffering to the number of times you are seeing if an item shows up in lst. In this case your print() test has n equal to 3:

print(more_than_n([2, 4, 6, 2, 3, 2, 1, 2], 2, 3))
                                               ^

Since you are wanting to see if item shows up more than n times, can you think of a way to check how many of item are in lst?

Hint
lst = [1, 1, 1, 2, 3, 3, 1, 3]
print(lst.count(1)) #prints 4 

In this case we’d assume n is a stand-in for any positive integer which is used to test how many times item can appear in a list. For a brief description of what’s expected see below-

Lets define a quick list sequence-
lst = ['a', 'b', 'b', 'b', 'c']

Your function should test if item appears more than n times…
So; if item = 'a' and n = 2 it should return False.
# 'a' does not appear more than twice.

If item = b and n = 2 it should return True.
# 'b' appears more than twice.

If item = 'b' and n = 3 it should return False.
# ‘b’ does not appear more than three times.

1 Like

@8-bit-gaming @tgrtim

Thank you for the explanation and I completely forgot about the count function.

I’ve added the following:

#Write your function here

def more_than_n(lst, item, n):
  length_list = lst.count(n)
  if item > length_list:
    return True
  else:
    False


#Uncomment the line below when your function is done
print(more_than_n([2, 4, 6, 2, 3, 2, 1, 2], 2, 3))

I feel like this is close but it’s saying that it is wrong for a specific result:

more_than_n([2, 3, 4], 2, 1) should have returned False , and it returned True

I guess because 2 appears twice in the above scenario. However I’m not sure where I went wrong in my code.

Well one of the best ways to debug your code is to try using print() inside your function.

Try printing out different variables like item and length_list to see what those are currently equal to.

1 Like

@8-bit-gaming

It’s outputting

2 1
None

#Write your function here

def more_than_n(lst, item, n):
  length_list = lst.count(n)
  if item > length_list:
    return print(item, length_list)
  else:
    False


#Uncomment the line below when your function is done
print(more_than_n([2, 4, 6, 2, 3, 2, 1, 2], 2, 3))

It’s also saying that it should return false for the following result set:

more_than_n([2, 3, 4], 2, 1) should have returned False, and it returned True

It’s really not clear what values are being plugged into n or item or length_list at all.

I can’t decipher how/why the result is wrong. It looks right, 2 is greater than 1 so it returned True.

Perhaps I should have been clearer earlier. Currently lst is the list being fed into the function, item is what you are checking for, and n is the number of times you are checking to see if item shows up.

So if item is what you are looking for, and n is the number of times it should show up, than lst.count(n) is going to be how many times 3 is in the list.

Here is a pseudo code anology.

Currently you are checking:
if item is more than the amount of times n is in lst.

Were as you are supposed to check:
if the amount of times item is in lst is more than n.

@8-bit-gaming

Okay I had a bit of a breakthrough there, thank you for the explanation. The print function that it is running is making a lot more sense.

I feel as if I’m so close now. It still is returning None. I would expect to at least return False.

#Write your function here

def more_than_n(lst, item, n):
  n = lst.count(item)
  if item > n:
    return True
  else:
    False

#Uncomment the line below when your function is done
print(more_than_n([2, 4, 6, 2, 3, 2, 1, 2], 2, 3))

It is because since the if gate evaluates to False, and you don’t have anything returned from there.

If you don’t know you, can use multiple return statements in a function:

def boolean_binary(boolean): 
  if boolean >= 1:
    return True
  else: 
    return False

print(boolean_binary(1)) #prints True

Holy smokes I think I got it:

#Write your function here

def more_than_n(lst, item, n):
  test = lst.count(item)
  if test > n:
    return True
  else:
    return False

#Uncomment the line below when your function is done
print(more_than_n([2, 4, 6, 2, 3, 2, 1, 2], 2, 3))

Thanks for your help! @8-bit-gaming. That felt good to finally figure out. I think I learned a lot on this one. Again thank you so much.

1 Like