1

I am fairly new to python, I am not sure on how to fix a index string out of range. it happens right after the while loop when I want to send mylist[i][0] to formatting function. Any pointer on my code in general would be awesome!

    def formatting(str1):

if str1 == '?':
    return True
else:
    return False

while(i <= len(mylist)):
    val = formatting(mylist[i][0])
    if val == True:
        str1 = mylist[i]
        str2 = mylist[i+1]
        i = i + 2
        format_set(str1, str2)
    else:
        if format == True:
            if (margin + count + len(mylist[i])) <= width:
                if (i == (len(mylist)-1)):
                    list2.append(mylist[i])
                    print(" " * margin + " ".join(list2))   
                    break
            list2.append(mylist[i])
            count += len(mylist[i])
            i += 1                          
        else:
            print(" " * margin + " ".join(list2))
            list2 = []
            count = 0
    else:
        temp_margin = margin
        temp_width = width
        width = 60
        margin = 0
        if (margin + count + len(mylist[i])) <= width:
            if (i == (len(mylist)-1)):
                list2.append(mylist[i])
                print(" " * margin + " ".join(list2))
                margin = temp_margin
                width = temp_width
                break
            list2.append(mylist[i])
            count += len(mylist[i])
            i += 1                          
        else:
            print(" " * margin + " ".join(list2))
            list2 = []
            count = 0
1
  • Use 4 spaces to introduce correct indentation in your code. TAB doesn't work for this here. Commented Jun 26, 2014 at 3:25

2 Answers 2

1

change

i <= len(mylist)

to

i < len(mylist)
Sign up to request clarification or add additional context in comments.

1 Comment

you'll still have to deal with the mylist[i+1] that Abhi mentioned.
1

In the last iteration of the while loop, i is referring to the last value. Hence,

str2 = mylist[i+1]

is trying to reference a string outside the allowed range and you get an error.


EDIT: Also, as Wcrousse mentioned, the while (i <= len(...)) should be changed to i < len(...) because indexes go from 0 - (length-1).

4 Comments

I get the error on the line val = formatting(mylist[i][0]), it doesn't get a chance to make it to the str2 = mylist[i+1]
Post your definition of formatting(). It is likely making the same mistake within.
Wcrousse's answer was the main reason for your issue. Using i <= len(mylist) would push the last iteration of i outside the range.
with changing it to i < len(mylist) I still get the error. for the str2 = mylist[i+1] if it makes it to that point there is for sure a i+1

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.