count..Please help me in the below query


#1

def count(sequence,item):
 newSeq=list(sequence)
 size=len(newSeq)
 print "newList", newSeq ,"size ",size 
 result=0
 for i in range(0,size-1):
  if (item==newSeq[i]):
      print "I am in if loop: ",newSeq[i]
      result=result+1
 print "result",result
 return result

My function should return result value,But its just returning 0.
No idea whats happening


#2

Do you call your function?


#3

Yes.While printing,Its printing the correct result..But not working when its returning


#4

I don't understand what you mean, if it is about the print and return statements in your function you must take into account that print and return statements work differently :slight_smile:


#5

Can you please tell me where I am wrong


#7

i had the same issue. I changed the for loop from len(sequence) rather than len(sequence)-1

you need to change your for loop.

for i in range(0,size):

Also, we shouldnt use list function in this execution. You can directly say len(sequence)


#8

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