Question

The file named world_series_winners.txt contains a chronological list of the World Series winning teams from 1903...

The file named world_series_winners.txt contains a chronological list of the World Series winning teams from 1903 through 2009. (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 2009. Note that the World Series was not played in 1904 or 1994.)

Write a program q3.py that lets the user enter the name of a team and then displays to the screen the number of times that team has won the World Series in the time period from 1903 through 2009. Your program calls a function count_wins that takes a name of team and a handle to the file as parameter and returns how many time this teams won.

txt:

"I programed with C for 15 years, C++ for 10+ years, Java for 4 years and Python for 3 years. Through C is not an object oriented language , it's still my 1st preference"

Homework Answers

Answer #1

main.py

def main():
    ws_winners = build_world_series_dictionary('world_series.txt')
    user_year = int(input('Enter year of World Series to see winner (1903-2013): '))
    user_year = verify_ws_year(user_year)

    team = ws_winners[user_year]
    print('Winner in ' + str(user_year) + ': ' + team)

    num_wins = count_wins(team, ws_winners)
    print('The ' + team + ' have won ' + str(num_wins))

    if num_wins == 1:
        print(' time.')
    else:
        print(' times.')


def build_world_series_dictionary(filename):
    file = open(filename, 'r')
    world_series_winners = {}
    line = file.readline().rstrip('\n')

    while line != '':
        line_list = line.split(',')
        team = line_list.pop()
        year = int(line_list.pop())
        world_series_winners[year] = team
        line = file.readline().rstrip('\n')

    return world_series_winners


def verify_ws_year(year):
    first_year = 1903
    latest_year = 2013
    while year < first_year or year > latest_year:
        year = int(input('Invalid year.\nEnter year of World Series to see winner: '))

    return year


def count_wins(team, ws_dict):
    count = 0

    for year in ws_dict:
        if ws_dict[year] == team:
            count += 1

    return count


main()

world_series.txt

1903,Boston Americans
1904,No World Series
1905,New York Giants
1906,Chicago White Sox
1907,Chicago Cubs
1908,Chicago Cubs
1909,Pittsburgh Pirates
1910,Philadelphia Athletics
1911,Philadelphia Athletics
1912,Boston Red Sox
1913,Philadelphia Athletics
1914,Boston Braves
1915,Boston Red Sox
1916,Boston Red Sox
1917,Chicago White Sox
1918,Boston Red Sox
1919,Cincinnati Reds
1920,Cleveland Indians
1921,New York Giants
1922,New York Giants
1923,New York Yankees
1924,Washington Senators
1925,Pittsburgh Pirates
1926,St. Louis Cardinals
1927,New York Yankees
1928,New York Yankees
1929,Philadelphia Athletics
1930,Philadelphia Athletics
1931,St. Louis Cardinals
1932,New York Yankees
1933,New York Giants
1934,St. Louis Cardinals
1935,Detroit Tigers
1936,New York Yankees
1937,New York Yankees
1938,New York Yankees
1939,New York Yankees
1940,Cincinnati Reds
1941,New York Yankees
1942,St. Louis Cardinals
1943,New York Yankees
1944,St. Louis Cardinals
1945,Detroit Tigers
1946,St. Louis Cardinals
1947,New York Yankees
1948,Cleveland Indians
1949,New York Yankees
1950,New York Yankees
1951,New York Yankees
1952,New York Yankees
1953,New York Yankees
1954,New York Giants
1955,Brooklyn Dodgers
1956,New York Yankees
1957,Milwaukee Braves
1958,New York Yankees
1959,Los Angeles Dodgers
1960,Pittsburgh Pirates
1961,New York Yankees
1962,New York Yankees
1963,Los Angeles Dodgers
1964,St. Louis Cardinals
1965,Los Angeles Dodgers
1966,Baltimore Orioles
1967,St. Louis Cardinals
1968,Detroit Tigers
1969,New York Mets
1970,Baltimore Orioles
1971,Pittsburgh Pirates
1972,Oakland Athletics
1973,Oakland Athletics
1974,Oakland Athletics
1975,Cincinnati Reds
1976,Cincinnati Reds
1977,New York Yankees
1978,New York Yankees
1979,Pittsburgh Pirates
1980,Philadelphia Phillies
1981,Los Angeles Dodgers
1982,St. Louis Cardinals
1983,Baltimore Orioles
1984,Detroit Tigers
1985,Kansas City Royals
1986,New York Mets
1987,Minnesota Twins
1988,Los Angeles Dodgers
1989,Oakland Athletics
1990,Cincinnati Reds
1991,Minnesota Twins
1992,Toronto Blue Jays
1993,Toronto Blue Jays
1994,No World Series
1995,Atlanta Braves
1996,New York Yankees
1997,Florida Marlins
1998,New York Yankees
1999,New York Yankees
2000,New York Yankees
2001,Arizona Diamondbacks
2002,Anaheim Angels
2003,Florida Marlins
2004,Boston Red Sox
2005,Chicago White Sox
2006,St. Louis Cardinals
2007,Boston Red Sox
2008,Philadelphia Phillies
2009,New York Yankees
2010,San Francisco Giants
2011,St. Louis Cardinals[W]
2012,San Francisco Giants
2013,Boston Red Sox


Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Python A text file (WorldSeriesWinners.txt ) contains a chronological list of the World Series winning teams...
Python A text file (WorldSeriesWinners.txt ) contains a chronological list of the World Series winning teams from 1903 through 2009. 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 2009. Note the World Series was not played in 1904 and 1994. Write a program to open the file WorldSeriesWinners.txt, read it line by line and store the team names in...
Do it in python please Boston Americans 1903 No Winner 1904 New York Giants 1905 Chicago...
Do it in python please Boston Americans 1903 No Winner 1904 New York Giants 1905 Chicago White Sox 1906 Chicago Cubs 1907 Chicago Cubs 1908 Pittsburgh Pirates 1909 Philadelphia Athletics 1910 Philadelphia Athletics 1911 Boston Red Sox 1912 Philadelphia Athletics 1913 Boston Braves 1914 Boston Red Sox 1915 Boston Red Sox 1916 Chicago White Sox 1917 Boston Red Sox 1918 Cincinnati Reds 1919 Cleveland Indians 1920 New York Giants 1921 New York Giants 1922 New York Yankees 1923 Washington Senators...
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation....
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation. case:    W17400 APIGEE: PEOPLE MANAGEMENT PRACTICES AND THE CHALLENGE OF GROWTH Ranjeet Nambudiri, S. Ramnarayan, and Catherine Xavier wrote this case solely to provide material for class discussion. The authors do not intend to illustrate either effective or ineffective handling of a managerial situation. The authors may have disguised certain names and other identifying information to protect confidentiality. This publication may not be...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary rivals? How will the acquisition of Reebok by Adidas impact the structure of the athletic shoe industry? Is this likely to be favorable or unfavorable for New Balance? 2- What issues does New Balance management need to address? 3-What recommendations would you make to New Balance Management? What does New Balance need to do to continue to be successful? Should management continue to invest...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT