1

I'm running a while loop in Python with this syntax:

while not endFound:
    if file[fileIndex] == ';':
        current = current + ';'
        contents.append(current)
        if fileIndex == lengthOfFile:
            endFound = True
    else:
        current = current + file[fileIndex]
    fileIndex = fileIndex + 1

And am getting this error in my console:

var(vari) = 0;terminal.write(vari);var(contents) = file.getContents('source.py');if(vari : 0) {    terminal.write('vari equals 0');}
Traceback (most recent call last):
  File "/home/ubuntu/workspace/source.py", line 30, in <module>
    splitFile(content)
  File "/home/ubuntu/workspace/source.py", line 22, in splitFile
    if file[fileIndex] == ';':
IndexError: string index out of range

> Process exited with code: 1

What happened?

2 Answers 2

2

I'm assuming you have something like this before you begin:

file = "section_1;section_2;section_3;"
lengthOfFile = len(file)
contents = []
current = ""
fileIndex = 0
endFound = False

The code you wrote could be slightly clarified as follows:

while not endFound:
    next_char = file[fileIndex]
    current = current + next_char
    if next_char == ';':
        contents.append(current)
        #? current = ''
        if fileIndex == lengthOfFile:
            endFound = True
    fileIndex = fileIndex + 1

The problem in this particular case is that when you reach the final ; in file, fileIndex is 17, but lengthOfFile is 18. So the fileIndex == lengthOfFile test fails. You could fix the code above by changing this line to fileIndex + 1 == lengthOfFile, or by moving the increment operation above if next_char == ';'.

But there are simpler ways to write this code in Python. In particular, if your goal is for contents to become a list of all the "section_n;" entries in file, you could just use something like this:

contents = [part + ';' for part in file[:-1].split(';')]

(The [:-1] omits the last character (;) from file before splitting it.) Note that if this is what you want, then your original code would also need to reset the value of current during each pass, as noted above.

If you actually want contents to become a list of longer and longer substrings from the start of file, as currently written, you could do something like this:

contents1 = file[:-1].split(';')
contents = []
for part in contents1:
    current = current + part + ';'
    contents.append(current)
Sign up to request clarification or add additional context in comments.

Comments

1
  1. What is the value of fileIndex before you enter the loop?

  2. Check of the end of the string (if fileIndex == lengthOfFile:) is inside of if file[fileIndex] == ';':, so if there's no ; in the string, you effectively have infinite loop.

  3. Manipulations with characters are not very pythonic, there are most probably more effective tools to do what you (like .index method of str and so on.)

2 Comments

Actually, tested it out, no, it didn't. :(
1. The value of fileIndex starts as 0 and increases until it equals the length ot the file. (This script is being used to split lines in codes for me to read easier, so the semi-colons signify the end of the line.) Is that the problem? An infinite loop? the string is this: "var(vari) = 0; terminal.write(vari); var(contents) = file.getContents('source.py'); if(vari : 0) { terminal.write('vari equals 0'); };" There is an ending semi-colon though.

Your Answer

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