0

Every time I try to run my code and open a specific text document which is fairly long, it says string index out of range. I was wondering if any of you could help me solve this problem. I have to get my code to spit out more than what I have it doing right now, but this is a start and I didn't figure it wise to carry on without fixing the initial error.

file = open(input("Enter File Name:"))
lines = file.readlines()
file.close()
number_chars = 0
number_words = 0
largest_word = ""
smallest_word = ""
count_words = 0
largest = int(0)
smallest = int(999999)
average = 0
count = 0
sum = 0
for item in lines:
    is_word = False
    item = item.strip()
    char = item[0]
    count_words = count_words + 1
    if char >= 'a' and char <= 'z':
        is_word = True
    elif char >= 'A' and char <= 'Z':
        is_word = True
    if is_word == True:
        if(len(largest_word) < len(item)):
            largest_word = item
        if(len(smallest_word) > len(item)):
            smallest_word = item
        number_chars += len(item)
    else:
        item = int(item)
        count = count + 1
        sum = sum + item
        if(count == 1 or item > largest):
            largest = item
        if(count == 1 or item < smallest):
            smallest = item
print("Reading", "name_list.txt")
print("found",count,"numbers")
print("largest number :", largest)
print("smallest numer :", smallest)
Average = sum // count
print("Average :", Average)

print(largest_word)
print(smallest_word)

1 Answer 1

2

The first thing I'd be thinking about is the effect an empty line is going to have on the statements (primarily the last one):

file = open(input("Enter File Name:"))
lines = file.readlines()
for item in lines:
    item = item.strip()
    char = item[0]

An empty line will not have an index zero.

In terms of fixing it, it depends on your intent. From a casual glance, it appears that you treat each line as either a word or a number (though you may want to consider what happens if the line starts with :, for example - that won't be considered a word by your code and it's almost certainly going to cause problems when treating it as an integer).

However, putting that aside for now: since a blank line is neither a word nor a number, you may find the simplest fix is to simply ignore those blank lines totally, with something like:

item = item.strip()   # This line already exists.
if item == "":
    continue

A blank item will simply cycle around and get the next line.

However, as mentioned, this will only fix your immediate problem, you really should consider handling things that are not in the set {word, number}.

Sign up to request clarification or add additional context in comments.

2 Comments

So I need to remove the char = item[0]?
@Shane, see the update, easiest solution for that particular case is probably just to ignore blank lines totally.

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.