11.count


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

11.count

<In what way does your code behave incorrectly? Include ALL error messages.>

Does your count function take exactly two arguments: a list and an item to check against that list? Your code threw a “‘int’ object is not iterable” error.

<What do you expect to happen instead?>
It should have returned how ever many 'item’s there were in the sequence



def count(sequence, item):
  counter = 0
  for i in counter:
    if i == item:
      counter = counter + 1
      return counter
  return counter


#2

Hello @paulcherian

Does your count function take exactly two arguments: a list and an item to check against that list? Your code threw a “‘int’ object is not iterable” error.

Here you have to walk through each element of your sequence but you’re iterating through counter variable which is of int type (And It’s not iterable, Only sequence type are iterable) that’s why you got the error.

The general approach would be…

1.Set a counter variable and set it to zero
2.iterate through sequence and test if desirable item is in it or not.
3.If yes then increase counter variable by one.
4. At last return the counter variable as it has frequency of that particular item


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.