In: Computer Science
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file contains a chronological list of the World Series' winning teams from 1903 through 2018. 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 2018. (Note the World Series was not played in 1904 and 1994. There are entries in the file indicating this.)
Write a program that reads this file and creates TWO dictionaries. The keys of the first dictionary are the names of the teams, and each key's associated value is the number of times the team has won the World Series. The keys of the second dictionary are the years, and each key's associated value is the name of the team that won that year.
Then display the first dictionary in a sorted one with the team won the most of times the 1st row. A sample looking could be as:
List of World Series champions:
New York Yankees : 26 Times
St. Louis Cardinals : 11 Times
Boston Red Sox : 7 Times
New York Giants : 5 Times
Pittsburgh Pirates : 5 Times
Philadelphia Athletics : 5 Times
Cincinnati Reds : 5 Times
Los Angeles Dodgers : 5 Times
Detroit Tigers : 4 Times
Oakland Athletics : 4 Times
Chicago White Sox : 3 Times
…
The program should then prompt the user to enter a year in the range of 1903 through 2018. It should then display the name of the team that won the World Series that year, and the number of times that team has won the World Series. The program should allow a user to play multiple times. Remind user how to terminate the program.
Tips:
Text file titled: WorldSeries_1903_2018.txt
contents include:
Boston Americans World Series Not Played in 1904 New York Giants Chicago White Sox Chicago Cubs Chicago Cubs Pittsburgh Pirates Philadelphia Athletics Philadelphia Athletics Boston Red Sox Philadelphia Athletics Boston Braves Boston Red Sox Boston Red Sox Chicago White Sox Boston Red Sox Cincinnati Reds Cleveland Indians New York Giants New York Giants New York Yankees Washington Senators Pittsburgh Pirates St. Louis Cardinals New York Yankees New York Yankees Philadelphia Athletics Philadelphia Athletics St. Louis Cardinals New York Yankees New York Giants St. Louis Cardinals Detroit Tigers New York Yankees New York Yankees New York Yankees New York Yankees Cincinnati Reds New York Yankees St. Louis Cardinals New York Yankees St. Louis Cardinals Detroit Tigers St. Louis Cardinals New York Yankees Cleveland Indians New York Yankees New York Yankees New York Yankees New York Yankees New York Yankees New York Giants Brooklyn Dodgers New York Yankees Milwaukee Braves New York Yankees Los Angeles Dodgers Pittsburgh Pirates New York Yankees New York Yankees Los Angeles Dodgers St. Louis Cardinals Los Angeles Dodgers Baltimore Orioles St. Louis Cardinals Detroit Tigers New York Mets Baltimore Orioles Pittsburgh Pirates Oakland Athletics Oakland Athletics Oakland Athletics Cincinnati Reds Cincinnati Reds New York Yankees New York Yankees Pittsburgh Pirates Philadelphia Phillies Los Angeles Dodgers St. Louis Cardinals Baltimore Orioles Detroit Tigers Kansas City Royals New York Mets Minnesota Twins Los Angeles Dodgers Oakland Athletics Cincinnati Reds Minnesota Twins Toronto Blue Jays Toronto Blue Jays World Series Not Played in 1994 Atlanta Braves New York Yankees Florida Marlins New York Yankees New York Yankees New York Yankees Arizona Diamondbacks Anaheim Angels Florida Marlins Boston Red Sox Chicago White Sox St. Louis Cardinals Boston Red Sox Philadelphia Phillies New York Yankees San Francisco Giants St. Louis Cardinals San Francisco Giants Boston Red Sox San Francisco Giants Kansas City Royals Chicago Cubs Houston Astros Boston Red Sox
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. Thank You !! =========================================================================== def read_file(filename): team_win_dict = {} year_team_dict = {} year = 1903 try: with open(filename, 'r') as infile: for line in infile.readlines(): line = line.strip() if len(line) == 0: continue if team_win_dict.get(line) is None: team_win_dict[line] = 1 else: team_win_dict[line] += 1 year_team_dict[year] = line year += 1 return team_win_dict, year_team_dict except: return None, None def main(): filename = 'WorldSeries_1903_2018.txt' team_win_dict, year_team_dict = read_file(filename) if team_win_dict is None: print('Error: Unable to read data from file: {}'.format(filename)) return sorted_wins = sorted(team_win_dict.items(), key=lambda pair: pair[1], reverse=True) for team, wins in sorted_wins: print('{} : {} Times'.format(team, wins)) while True: year = int(input('Enter year: ')) if year == 1904 or year == 2018: print(year_team_dict.get(year)) else: if year_team_dict.get(year): winner = year_team_dict.get(year) print('Team: {} won the Word Series'.format(winner)) print('{} won {} Times'.format(winner, team_win_dict.get(winner))) else: print('No data found') repeat = input('Do you want to repeat (yes/no): ') if repeat == 'no': break main()
========================================================================
let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please rate the answer.
Thanks !
===========================================================================