How do you use a for loop to sort a list from a text file and count the number of times it matches a phrase entered by the user?

advertisements

The file WorldSeriesWinners.txt contains a chronological list of the World Series winning teams from 1903 through 2014. The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2014. Note that the World Series was not played in 1904 or 1994.

Write a program that lets the user enter the name of a team and then displays the number of times that team has won the World Series since 1903.

Sample program runs are as follows:


Enter team name: St. Louis Cardinals

St. Louis Cardinals won 11 times


Enter team name: Kansas City Royals

Kansas City Royals won 1 times


Enter team name: San Francisco Giants

San Francisco Giants won 3 times


Enter team name: Texas Rangers

Texas Rangers won 0 times


Format the ouput of your program as shown in the sample program runs. Design your program so that it contains at least one function other than the main function. No code should appear outside functions.

Hint: You may want to read the contents of WorldSeriesWinners.txt into a list. When the user enters the name of a team, use a loop to step through the list, counting the number of times the selected team appears.

What I have so far:

def readfile():

f = open("WorldSeriesWinners.txt", "r")
lineList = f.readlines()
f.close()

List = []

for line in lineList:
    line = line.strip()
    List.append(line)

def main():

    name = input("Enter team name: ")
    readfile()
main()


It's easy to let the user ask about several teams in one run:

import collections

def main():
    with open('WorldSeriesWinners.txt') as f:
        count = collections.Counter(l.strip() for l in f)
    while True:
        team = raw_input('Enter team name (empty to exit):')
        if not team: break
        print('{} won {} times'.format(team, count[team]))

That's all of the (enhanced) program -- no need for multiple gyrations making lists!-)

Added: the OP makes some absolutely weird requests in comments -- (A) "do that in at least 2 functions" (why?! ah well, crazy pointless, but innocuous), (B) "simpler code, like a for loop instead" (it wouldn't of course be any simpler than the l.strip() for l in f genexp I'm using above -- indeed, more complicated -- so here I draw the line and simply refuse!), (C) "break at the end of each search of a name" (evil, as it steals functionality from the user, but ah well, that was the original spec, so I'm going to bend and accommodate this one). So here's a variant satisfying A and C (but not B, never B!!!):

import collections

def count_wins():
    with open('WorldSeriesWinners.txt') as f:
        count = collections.Counter(l.strip() for l in f)
    return count

def main():
    count = count_wins()
    team = raw_input('Enter team name:')
    print('{} won {} times'.format(team, count[team]))

There -- two more lines, a bit less functionality. How that's supposed to be an "improvement", well, this really I couldn't tell you!-)

Hint for seekers of (B): that requires adding 2 more statements, and a little study of https://docs.python.org/2/library/collections.html#collections.Counter -- bah!