Question

In: Computer Science

In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file...

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:

  • Must have at least one function created by you in the program
  • Document/comment your program
  • Be user friendly
  • Design your interface
  • Format your output

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

Solutions

Expert Solution

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 !


===========================================================================


Related Solutions

Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
In this assignment you will write a program that encrypts a text file.  You will use the...
In this assignment you will write a program that encrypts a text file.  You will use the following encryption scheme. Ask the user for the name of the original file. Ask the user for the name of the output file. Ask the user for the encryption key, n. Read n2 characters from the file into the n rows and n columns of a 2-dimensional array. Transpose the array.  (Exchange the rows and columns.) Write the characters from the array to an output...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file must satisfy the following. Define a function named rinsert. This function will accept two arguments, the first a list of items to be sorted and the second an integer value in the range 0 to the length of the list, minus 1. This function shall insert the element corresponding to the second parameter into the presumably sorted list from position 0 to one less...
Write a code to find the following in a text file (Letter). language: Python (a) Find...
Write a code to find the following in a text file (Letter). language: Python (a) Find the 20 most common words (b) How many unique words are used? (c) How many words are used at least 5 times? (d) Write the 200 most common words, and their counts, to a file. text file: Look in thy glass and tell the face thou viewest, Now is the time that face should form another, Whose fresh repair if now thou not renewest,...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below...
Write a python program that does the following: Prompt for a file name of text words....
Write a python program that does the following: Prompt for a file name of text words. Words can be on many lines with multiple words per line. Read the file and convert the words to a list. Call a function you created called list_to_once_words(), that takes a list as an argument and returns a list that contains only words that occurred once in the file. Print the results of the function with an appropriate description. Think about everything you must...
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
Exercise 2: You are provided with a text file named covid19-3.txt. It reports a few confirmed...
Exercise 2: You are provided with a text file named covid19-3.txt. It reports a few confirmed cases of covid19. It consists of three columns. The 1st column indicates the names of the provinces, the 2nd column indicates the names of the countries and the 3rd column indicates the numbers of confirmed cases. To do: 1. Define a function that reads, from covid19-3.txt, provinces, countries and confirmed cases in three separate lists. 2. Define a function that iterates through a list...
Consider a text file that you will create named “employees.txt”. The file contains data organized according...
Consider a text file that you will create named “employees.txt”. The file contains data organized according to the following format:John Smith 10 15Sarah Johnson 40 12Mary Taylor 27 13Jim Stewart 25 8For instance, “John” is the first name, “Smith” is the last name, “10” is the number of hours per week, and “15” is the hourly rate.Write a program that computes the weekly salary of each employee. The program prints the first name, last name, and weekly salary of each...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT