0

I want to find how many times an character repeated in a word for example: how many times the 'l' repeated in the "Hello". but it drops me this error:

IndexError: string index out of range

And my code:

def fin(x,lst):
   if x == '':
      return 0
   if lst[0] == x:
      return 1+ fin(x,lst[1:])
   else:
      return fin(x,lst[1:])
2
  • how many items in lst when this error occurs? Maybe you had only 1 item, and the lst[1:] slicing generated this error Commented Oct 15, 2014 at 18:54
  • Hint: what happens if I call fin("X", "")? Commented Oct 15, 2014 at 18:54

1 Answer 1

4

There are pre-written functions in python to do this for you. But if you're doing this for the sake of education...

You have the wrong stop condition, you should be checking if the lst is empty, not x

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.