My python program splits only the last paragraph of my text file, does someone know why this happens?

StackOverflow https://stackoverflow.com/questions/22587859

Question

ok, so I'm trying to solve this exercise about finding words in a text file and it should print how many words are. So what I try to do is splitting the words first so it's easier to look for specific words. However, when I try to split two paragraphs, it only splits the second one.

This is what the text file says:

"Bill & Ted's Excellent Adventure is a 1989 American science fiction comedy buddy film and the first film in the Bill & Ted franchise in which two slackers travel through time to assemble a menagerie of historical figures for their high school history presentation.

The film was written by Chris Matheson and Ed Solomon and directed by Stephen Herek. It stars Keanu Reeves as Ted "Theodore" Logan, Alex Winter as Bill S. Preston, Esquire, and George Carlin as Rufus. Bill & Ted's Excellent Adventure received reviews which were mostly positive upon release and was commercially successful. It is now considered a cult classic. A sequel, Bill & Ted's Bogus Journey, was released two years later. An untitled third film is in development"

The idea is to look for how many "Bill"s are in the paragraphs.

f = open("text.txt", "r")
listBill = []
for line in f:
    print(line)

splitParagraph = line.split()
print(splitParagraph)
for eachBill in splitParagraph:
    if eachBill == "Bill":
        listBill.append("Bill")
print(listBill)

But this is what I get instead:

Bill & Ted's Excellent Adventure is a 1989 American science fiction comedy buddy film 
and the first film in the Bill & Ted franchise in which two slackers travel through time
to assemble a menagerie of historical figures for their high school history resentation.

The film was written by Chris Matheson and Ed Solomon and directed by Stephen Herek. It
stars Keanu Reeves as Ted "Theodore" Logan, Alex Winter as Bill S. Preston, Esquire, and
George Carlin as Rufus. Bill & Ted's Excellent Adventure received reviews which were 
mostly positive upon release and was commercially successful. It is now considered a 
cult classic. A sequel, Bill & Ted's Bogus Journey, was released two years later. An 
untitled third film is in development

['The', 'film', 'was', 'written', 'by', 'Chris', 'Matheson', 'and', 'Ed', 'Solomon',  
'and', 'directed', 'by', 'Stephen', 'Herek.', 'It', 'stars', 'Keanu', 'Reeves', 'as', 
'Ted', '"Theodore"', 'Logan,', 'Alex', 'Winter', 'as', 'Bill', 'S.', 'Preston,',  
'Esquire,',    'and', 'George', 'Carlin', 'as', 'Rufus.', 'Bill', '&', "Ted's",     
'Excellent',     'Adventure', 'received', 'reviews', 'which', 'were', 'mostly',      
 positive', 'upon', 'release', 'and', 'was', 'commercially', 'successful.', 'It', 'is',
 now', 'considered', 'a', 'cult', 'classic.', 'A', 'sequel,', 'Bill', '&', "Ted's", 
 Bogus', 'Journey,', 'was', 'released', 'two', 'years', 'later.', 'An', 'untitled', 
 'third', 'film', 'is', 'in', 'development']

['Bill', 'Bill', 'Bill']

As you can see, it found only 3 "Bill"s because it only read the second paragraph. I tried searching for people with the same problem as me, but it seems I'm the only one. Note that the program could print the first two paragraphs without splitting.

Was it helpful?

Solution

Your indentation is wrong, should be:

f = open("text.txt", "r")
listBill = []
for line in f:
    print(line)

    splitParagraph = line.split()
    print(splitParagraph)
    for eachBill in splitParagraph:
        if eachBill == "Bill":
            listBill.append("Bill")

print(listBill)

OTHER TIPS

There's a much easier way to look for words in a string. Just use the string's count method:

s = """Bill & Ted's Excellent Adventure is a 1989 American science fiction comedy buddy film and the first film in the Bill & Ted franchise in which two slackers travel through time to assemble a menagerie of historical figures for their high school history presentation.

The film was written by Chris Matheson and Ed Solomon and directed by Stephen Herek. It stars Keanu Reeves as Ted "Theodore" Logan, Alex Winter as Bill S. Preston, Esquire, and George Carlin as Rufus. Bill & Ted's Excellent Adventure received reviews which were mostly positive upon release and was commercially successful. It is now considered a cult classic. A sequel, Bill & Ted's Bogus Journey, was released two years later. An untitled third film is in development"""

print(s.count("Bill"))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top