7 Replies - 6280 Views - Last Post: 29 December 2012 - 01:00 AM Rate Topic: -----

#1 Highest bidder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 23-November 12

problem with IndexError: string index out of range

Posted 25 December 2012 - 03:50 AM

So my code is the following:

def correction(search_str, target_str):
	i = 0
	new_string = ''
	while True:
		if search_str.find(target_str[i]) != -1:
			new_string = new_string + target_str[i]
		else: break
		i = i + 1
	return new_string



What i am trying to do is to find some spread letters in a text(search_str) and form a word(target_str) out of them if it exists. How ever i keep on getting "IndexError: string index out of range" for the line:

search_str.find(target_str[i]) != -1:


what am i doing wrong?

Kind regards

Is This A Good Question/Topic? 0
  • +

Replies To: problem with IndexError: string index out of range

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: problem with IndexError: string index out of range

Posted 25 December 2012 - 04:10 AM

You keep incrementing variable "i" and at some point it will move out of range. You need to include some bounds checks.
Was This Post Helpful? 2
  • +
  • -

#3 Highest bidder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 23-November 12

Re: problem with IndexError: string index out of range

Posted 25 December 2012 - 08:47 AM

Thanks very much! It would seem more reasonable if i had to reach the limit before i get the error, but whatever... Thanks again, you saved me from a lot of trouble...
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2015
  • View blog
  • Posts: 3,041
  • Joined: 21-June 11

Re: problem with IndexError: string index out of range

Posted 25 December 2012 - 09:48 AM

View PostHighest bidder, on 25 December 2012 - 04:47 PM, said:

It would seem more reasonable if i had to reach the limit before i get the error


You do have to reach the limit before you get an error.
Was This Post Helpful? 1
  • +
  • -

#5 alexr1090  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 108
  • Joined: 08-May 11

Re: problem with IndexError: string index out of range

Posted 28 December 2012 - 02:11 AM

View PostHighest bidder, on 25 December 2012 - 04:50 AM, said:

How ever i keep on getting "IndexError: string index out of range" for the line:

Basically what's happening is that sometimes your program is continually finding a match for target_str[i] until 'i' is greater than the size of 'target_str'. When i gets to be greater than the length of target_str, it's raising the error.

To fix it I'd probably change the while True to
while x<=len(target_str)-1:
and leave the rest the same. However if you'd like to you could leave it as while True and use a try except block. For example
def correction(search_str, target_str):
	i = 0
	new_string = ''
	while True:
                try:
                    if search_str.find(target_str[i]) != -1:
                         new_string = new_string + target_str[i]
		    else: break
                    i = i + 1
                except IndexError:
                     break
	return new_string


This post has been edited by alexr1090: 28 December 2012 - 02:13 AM

Was This Post Helpful? 1
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2015
  • View blog
  • Posts: 3,041
  • Joined: 21-June 11

Re: problem with IndexError: string index out of range

Posted 28 December 2012 - 06:13 AM

View Postalexr1090, on 28 December 2012 - 10:11 AM, said:

To fix it I'd probably change the while True to
while x<=len(target_str)-1:


If we need the index, I'd prefer for x in range(len(target_str)). I mean, the index increases by exactly one on each iteration of the loop (except when the loop is broken out of), so why not make that obvious (and unbreakable) by using a construct that says "iterate up to the length, by 1".

However we don't actually need the index at all (it's only ever used to index into target_str and no arithmetic or checks are performed on it, other than to increment it. So why not just use a normal for loop and get rid of i altogether?
Was This Post Helpful? 2
  • +
  • -

#7 alexr1090  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 108
  • Joined: 08-May 11

Re: problem with IndexError: string index out of range

Posted 28 December 2012 - 07:40 AM

View Postsepp2k, on 28 December 2012 - 07:13 AM, said:

However we don't actually need the index at all (it's only ever used to index into target_str and no arithmetic or checks are performed on it, other than to increment it. So why not just use a normal for loop and get rid of i altogether?


Becuase I'm not as good as a programmer as you. Thanks for showing me how to do it the right way.
Was This Post Helpful? 0
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: problem with IndexError: string index out of range

Posted 29 December 2012 - 01:00 AM

View Postsepp2k, on 28 December 2012 - 08:13 AM, said:

If we need the index, I'd prefer for x in range(len(target_str)).

If you need the index, I'd suggest
for index,value in enumerate(target_str):

This post has been edited by atraub: 29 December 2012 - 01:02 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1