0

I have three lists in this program. 'library' contains a large amount of text, 'references' contains a list of words that follow each other in references, and 'possibles' looks at every time that each item in 'references' appear together so it can get the following word.

The program gets the length of 'references' so it knows how many items to check in 'library', but the piece of code that checks if references has 2 items in it returns 'unexpected indention', and I can't figure out why. Even if I remove the indention, it returns the error. If it is not indented, there is an error because it disrupts the rest of the code.

Below is the entire function. The entire program works aside from the indention error.

def possibles_compile():
    compile = 'yes'
    global temp_possibles
    temp_possibles = []
    while compile != 'no':
        if len(reference) == 1:
            for i in range(library_length):
                try:
                    if library[i] == reference[0]:
                        try:
                            temp_possibles.append(library[i + 1])
                        except IndexError:
                            temp_possibles.append(library[0])
        elif len(reference) == 2:
            for i in range(library_length):
                if library[i] == reference[0]:
                    try:
                        if library[i + 1] == reference[1]:
                            try:
                                temp_possibles.append(library[i + 2])
                            except IndexError:
                                temp_possibles.append(library[0])
                    except IndexError:
                        if library[0] == reference[1]:
                                temp_possibles.append(library[1])
        elif len(reference) == 3:
            for i in range(library_length):
                if library[i] == reference[0]:
                    try:
                        library[i + 1] == reference[1]:
                    except IndexError:
                        if library[0] == reference[1]:
                            if library[1] == reference[2]:
                                    temp_possibles.append(library[2])
                    else:
                        try:
                            if library[i + 2] == reference[2]:
                                try:
                                    temp_possibles.append(library[i + 3])
                                except IndexError:
                                    temp_possibles.append(library[0])
                        except IndexError:
                            if library[0] == reference[2]:
                                temp_possibles.append(library[1])
        elif len(reference) == 4:
            for i in range(library_length):
                if library[i] == reference[0]:
                    try:
                        library [i + 1] == reference[1]:
                    except IndexError:
                        if library[0] == reference[1]:
                            if library[1] == reference[2]:
                                if library[2] == reference[3]:
                                    temp_possibles.append(library[3])
                    else:
                        try:
                            library[i + 2] == reference[2]:
                        except IndexError:
                            if library[0] == reference[2]:
                                if library[1] == reference[3]:
                                    temp_possibles.append(library[2])
                        else:
                            try:
                                if library[i + 3] == reference[3]:
                                    try:
                                        temp_possibles.append(library[i + 4])
                                    except IndexError:
                                        temp_possibles.append(library[0])
                            except IndexError:
                                if library[0] == reference[3]:
                                    temp_possibles.append(library[1])
        compile = 'no'
    return 'done'
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    In your first `if` block you have a `try` statement without a matching `except` (or `finally`) (that's line 7 of the code in your question). – larsks May 07 '20 at 02:21
  • 2
    And then later on you have some misplaced colons. See e.g. line 31. In general your life would be easier if you used an editor with syntax checking, because it could point these errors out to you. – larsks May 07 '20 at 02:23
  • I *cannot* believe I didn't catch that. I read through it like 20 times and missed it *thank you so much* – user12799479 May 07 '20 at 02:25
  • Welcome to SO! Check out the [tour]. Glad you figured it out :) In the future you need to make a [mre]. – wjandrea May 07 '20 at 02:39

2 Answers2

0

The comments opened my eyes to the fact that I had a try with no except and lots of misplaced colons. Removing the colons and try made it work flawlessly

0

Before the line:

if library[i] == reference[0]:

You created an exception handler but only put the try clause and every try must have an associated except to it. So I suggest you change your code to:

try:
    if library[i] == reference[0]:
    try:
        temp_possibles.append(library[i + 1])
    except IndexError:
        temp_possibles.append(library[0])
except Exception:
    pass # put the exception code you want to executed when handling this exception here

Or, you can remove that try clause if it's unnecessary.

Also, be careful with the following lines:

library[i + 1] == reference[1]:
library [i + 1] == reference[1]:
library [i + 1] == reference[1]:

You're using a colon on a place you shouldn't (unless you wanted to create a condition or an iterate clause).

arthursribeiro
  • 326
  • 3
  • 9