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"
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
Get Answers For Free
Most questions answered within 1 hours.