python 3.4 Counting occurrences in a .txt file
Asked Answered
A

3

5

I am writing a "simple" little program for a class i am taking. this is supposed ask me for what team to search for and then return the number of times it appears on the list in a .txt file. it requests input like it should and seems to run great! its been running for an hour now :) i am getting no errors at all it seems to be stuck in a loop. thank you all in advance for your help!

here is my code

count = 0

def main():
# open file
    teams = open('WorldSeriesWinners.txt', 'r')
# get input
    who = input('Enter team name: ')
#begin search
    lst = teams.readline()
    while lst != '':
        if who in lst:
            count += 1

teams.close()
print(count)

main()
Admittance answered 22/4, 2014 at 23:43 Comment(0)
F
13

You don't need to go through the file counting lines manually. You can just use .read():

count = lst.count(who)

The other problem is that you're calling teams.close() and print(count) outside of the function.

That means they'll try to execute before you call main, and you're trying to close 'teams' which hasn't been opened or defined yet, so your code doesn't know what to do. The same is with printing count - count hasn't been defined outside of the function, which hasn't been called.

If you want to use them outside the function, at the end of the function you need to return count

Also, in your loop, you're executing the statement count += 1 which means count = count + 1, but you haven't told it what count is the first time it runs, so it doesn't know what it should add to one. Fix this by defining count = 0 before the loop inside the function.

And the reason you have an infinite loop is because your condition will never be satisfied. Your code should never take an hour to execute, like, pretty much never. Don't just leave it running for an hour.

Here's some alternative code. Make sure you understand the problems though.

def main():

    file  = open('WorldSeriesWinners.txt', 'r').read()
    team  = input("Enter team name: ")
    count = file.count(team)

    print(count)

main()

You can literally put this entire program into one line:

print(open('WorldSeriesWinners.txt', 'r').read().count(input("Enter team name: ")))
Forgetmenot answered 22/4, 2014 at 23:55 Comment(1)
thanks, but how to count phrase like ("New folder")Concupiscence
W
0

According to the docs :https://docs.python.org/3/library/io.html#io.IOBase.readline, readline returns single line, so in your program you have infinite loop with first line of the file

while lst != ''

You can try something like

for line in teams:
    if who in line:
        count += 1
Woolsey answered 22/4, 2014 at 23:51 Comment(1)
i initialize the count to 0 in the first line {count = 0} is this improper placement? i thought it best as a global variable.Admittance
D
0

If you don't mind lowercase or uppercase, you can use this modified version of @charles-clayton response!

print(open('WorldSeriesWinners.txt', 'r').read().lower().count(input("Enter team name: ").lower()))
Downe answered 5/2, 2021 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.