Question

In: Computer Science

Python: The file, Program11.txt, on the I: drive contains a chronological list of the World Series’...

Python:

The file, Program11.txt, on the I: drive 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 that the World Series was not played in 1904 or 1994. There are no entries in the file indicating this.) Write a program that reads this file and creates a dictionary in which the keys 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 program should also create a dictionary in which the keys are the years and each key’s associated value is the name of the team that won that year. The program should prompt the user for 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.
Allow the user to run the program as many times as possible until a sentinel value of zero (0) has been entered for the year. No input, processing, or output should happen in the main function. All work should be delegated to other functions. The program should have at least 4 functions (main and developerInfo included). Include the recommended minimum documentation for each function. See the program one template for more details. Run your program at least five times with 1903, 1994, 2009, 2016, and 2018 as the user input for the year. Copy and paste the outputs to a file.

program11.txt:

Boston Americans
New York Giants
Chicago White Sox
Chicago Cubs
Chicago Cubs
Pittsburg 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
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

def main():
# Local dictionary variables
year_dict = {}
count_dict = {}
  
developerInfo()
  
# Open the file for reading
input_file = open('Program11.txt', 'r')


def showResults(year_dict, count_dict):
  
# Receive user input
year = int(input('Enter a year in the range 1903-2018: '))

# Print results
if year == 1904 or year == 1994:
print("The world series wasn't played in the year", year)
elif year < 1903 or year > 2018:
print('The data for the year', year, \
'is not included in our database.')
else:
winner = year_dict[year]
wins = count_dict[winner]
print('The team that won the world series in ', \
year, ' is the ', winner, '.', sep='')
print('They have won the world series', wins, 'times.')
  
# End of showResults

Solutions

Expert Solution

SOLUTION-
I have solve the problem in python code with comments and screenshot for easy understanding :)

CODE-

# python code
def read_file():
  
    file_read = open('Program11.txt', 'r')   # Open file to read

    # Create new dictionaries
    count_dict = {}
    year_dict = {}

    # Initial year
    year = 1903
    # For each name in file
    # Add it to key in count_dict if already not exists and set value 1
    # If exists, increment value by 1

    # Add year in years_dictionaries with name as value, increment year by 1
    # If year is 1904 or 1994, increment by another 1
    for name in file_read.readlines():
        team_name = name.rstrip()
        if team_name in count_dict.keys():
            count_dict[team_name] += 1
        else:
            count_dict[team_name] = 1

        year_dict[year] = team_name
        year += 1

        if year in [1904, 1994]:
            year += 1

    # Close file
    file_read.close()

    # return both dictionaries
    return count_dict, year_dict


def print_result(year, count_dict, year_dict):
    # Get team name from years dictionary
    # Print name and winning times
    team_name = year_dict[year]
    print('Team won in', year, 'is', team_name)
    print(team_name, 'won', count_dict[team_name], 'times.')


def get_year():
    # Ask user to enter year
    year = int(input('Enter year between 1903-2018 or 0 to quit: '))
    # If use enter 0, return
    if year is 0:
        return year

    # Check for invalid values and ask to enter valid
    while year < 1903 or year > 2018 or year in [1904, 1994]:
        year = int(input('Enter year between 1903-2018 or 0 to quit: '))

    # Return year
    return year


def main():
    count_dict, year_dict = read_file()

    year = get_year()

    # Iterate while till year is not 0
    while year is not 0:
        print_result(year, count_dict, year_dict)
        print()
        # Ask user to enter year
        year = get_year()


main()

--------Program11.txt--------

Boston Americans
New York Giants
Chicago White Sox
Chicago Cubs
Chicago Cubs
Pittsburg 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
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

SCREENSHOT-

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
Python: The attached file (books.csv) contains a list of some of the world's most famous books,...
Python: The attached file (books.csv) contains a list of some of the world's most famous books, including author, title and approximate year written/published. Your task is to write a program to do the following: Create a class "Book()" that can store the values passed as arguments at creation time as attributes called "title", "year", and "author". NOTE: Be sure to convert the year data to an integer, and to deal with any data that cannot be converted by setting the...
Write a program that asks the user for a file name. The file contains a series...
Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in the array 5)...
IN PYTHON File Data --- In file1.txt add the following numbers, each on its own line...
IN PYTHON File Data --- In file1.txt add the following numbers, each on its own line (20, 30, 40, 50, 60). Do not add data to file2.txt. Write a program. Create a new .py file that reads in the data from file1 and adds all together. Then output the sum to file2.txt. Add your name to the first line in file2.txt (see sample output) Sample Output Your Name 200 use a main function.
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching pairs of vehicle models and their respective makes Separate out the individual make and model on each line of the file Add the vehicle make to one list, and the vehicle model to another list; such that they are in the same relative position in each list Prompt the user to enter a vehicle model Search the list containing the vehicle models for a...
This is a python file Reads information from a text file into a list of sublists....
This is a python file Reads information from a text file into a list of sublists. Be sure to ask the user to enter the file name and end the program if the file doesn’t exist. Text file format will be as shown, where each item is separated by a comma and a space: ID, firstName, lastName, birthDate, hireDate, salary Store the information into a list of sublists called empRoster. EmpRoster will be a list of sublists, where each sublist...
#Java I am reading from the txt file and I put everytihng inside of the String[],...
#Java I am reading from the txt file and I put everytihng inside of the String[], like that: String[] values = str.split(", "); But, now I have to retrieve the data from it one by one. How can I do it? Here is the txt file: OneTime, finish the project, 2020-10-15 Monthly, wash the car, 2020-11-10 Thanks!
The file CO2.txt, found on Blackboard with this assignment, contains 50 numbers, which represent the concentration...
The file CO2.txt, found on Blackboard with this assignment, contains 50 numbers, which represent the concentration of atmospheric carbon dioxide (parts per million) recorded at Mauna Loa, HI. The data in the file are the CO2 values on May 15th of each year from 1961 through 2010, (with background level CO2 removed). Fit an exponential model. Use the model to predict the CO2 value for May 15, 2015. Print the result to the screen using fprintf. The actual value was...
I have a Python code that reads the text file, creates word list then calculates word...
I have a Python code that reads the text file, creates word list then calculates word frequency of each word. Please see below: #Open file f = open('example.txt', 'r') #list created with all words data=f.read().lower() list1=data.split() #empty dictionary d={} # Adding all elements of the list to a dictionary and assigning it's value as zero for i in set(list1):     d[i]=0 # checking and counting the values for i in list1:     for j in d.keys():        if i==j:           d[i]=d[i]+1 #Return all non-overlapping...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT