Habib Kazemi
Habib Kazemi

Reputation: 2190

string index out of range in Python

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:])

Upvotes: 0

Views: 106

Answers (1)

sedavidw
sedavidw

Reputation: 11691

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

Upvotes: 4

Related Questions