[Python] Don't understand why this program won't run

Note that I made a short list of words since I don't have your file of words to use. Here is a slightly reformatted version:

def is_alphabetical():
    # fin = open('words.txt') #words.txt is the list of words
    fin = ['words','and','more','words']
    index = 1
    for word in fin:
        while index < len(word):
            if ord(word[index]) >= ord(word[index - 1]):
                index += 1
            if index == len(word):
                print(word)
            else:
                break
        else:
                break

is_alphabetical()

The first thing is that comments are annotated with #

Now, if we change your break statements to print statements we can see that your while loop is infinite and it is getting to the break statement every time.

def is_alphabetical():
    # fin = open('words.txt') #words.txt is the list of words
    fin = ['words','and','more','words']
    index = 1
    for word in fin:
        while index < len(word):
            if ord(word[index]) >= ord(word[index - 1]):
                index += 1
            if index == len(word):
                print(word)
            else:
                # break
                print('First Break')
        else:
                # break
                print('Second Break')

is_alphabetical()

The problem is that ord() doesn't do what you think it does.

You need to split the word into a list of letters, use sorted(), then print the letters out.

/r/learnprogramming Thread