Question

PYTHON Text file Seriesworld attached below and it is a list of teams that won from...

PYTHON

Text file Seriesworld attached below and it is a list of teams that won from 1903-2018. First time mentioned won in 1903 and last team mentioned won in 2018. World series wasn’t played in 1904 and 1994 and the file has entries that shows it.

Write a python program which will read this file and create 2 dictionaries. The first key of the dictionary should show name of the teams and each keys value that is associated is the amount of times the team has won the series. The key of second dict. in the year and every single key’s associated value is the name of the team that were the winner that year.

Then display the 1st dict in a sorted one with the team that won the most times the first row. Here is an example:

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

Program should let the user enter a year between 1903-2018. Then it should show the team that won the series that year and how many times they won world series. It should let the user play multiple times and remind user to exit the program. The program must have one function created by you, comments should be included, be user friendly, and go ahead and design your interface.

sERIESWORLD.TXT

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
0 0
Add a comment Improve this question Transcribed image text
Answer #1
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 !!

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

# function takes in the filename, reads all the data from file
# function returns two dictionary one with name and frequency wins
# and the other with year and team name
# if the file could not be read function returns None

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 = 'sERIESWORLD.txt' # update the file name here
    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
         # sort the dictionary by frequency wins
    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')
         # ask user to repeat again
        repeat = input('Do you want to repeat (yes/no): ')
        if repeat == 'no':
            break


main()

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

thanks !

Add a comment
Know the answer?
Add Answer to:
PYTHON Text file Seriesworld attached below and it is a list of teams that won from...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 3.1 - 3.5 Certain machine parts are assigned serial numbers. Valid serial numbers are of...

    Python 3.1 - 3.5 Certain machine parts are assigned serial numbers. Valid serial numbers are of the form: SN/nnnn-nnn where ‘n’ represents a digit. Write a function valid_sn that takes a string as a parameter and returns True if the serial number is valid and False otherwise. For example, the input "SN/5467-231" returns True "SN5467-231" returns False "SN/5467-2231" returns False Using this function, write and test another function valid-sn-file that takes three file handles as input parameters. The first handle...

  • Java : I keep getting something wrong in my loadfile method, how do I fix my...

    Java : I keep getting something wrong in my loadfile method, how do I fix my code? Thank you. here is what is contained in the text file (WorldSeriesWinners.txt) 112 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...

  • write a SQL statements to perform the following: Select all years a World Series game was...

    write a SQL statements to perform the following: Select all years a World Series game was played Select all losing teams that played in a World Series game Select all winning and losing teams that played in a World Series game Select all cities of a winning or losing team that played in a World Series game Select all winning and losing teams that played in a World Series game, and provide aliases of "Winning Team" and "Losing Team" Select...

  • 3) American League baseball teams play their games with the designated hitter rule, meaning that pitchers...

    3) American League baseball teams play their games with the designated hitter rule, meaning that pitchers do not bat. The league believes that replacing the pitcher, typically a weak hitter, with another player in the batting order produces more runs. Using a significance level of a = 0.05, determine if the average number of runs is higher for the American League Following are the average number of runs scored by each team in the 2016 season: American League National League...

  • To be done on excel: Team Revenue ($ millions) Value ($ millions) Arizona Diamondbacks 195 584...

    To be done on excel: Team Revenue ($ millions) Value ($ millions) Arizona Diamondbacks 195 584 Atlanta Braves 225 629 Baltimore Orioles 206 618 Boston Red Sox 336 1,312 Chicago Cubs 274 1,000 Chicago White Sox 216 692 Cincinnati Reds 202 546 Cleveland Indians 186 559 Colorado Rockies 199 537 Detroit Tigers 238 643 Houston Astros 196 626 Kansas City Royals 169 457 Los Angeles Angels of Anaheim 239 718 Los Angeles Dodgers 245 1,615 Miami Marlins 195 520 Milwaukee...

  • Tampa Texas Toront 4-30 In 2012, the total payroll for the New York Yankees was almost...

    Tampa Texas Toront 4-30 In 2012, the total payroll for the New York Yankees was almost $200 million, while the total payroll for the Oakland Athletics (a team known for using baseball analytics or sabermetrics) was about $55 million, less than one-third of the Yankees' payroll. In the following table, you will see the payrolls (in millions) and the total number of victories for the baseball teams in the American League in the 2012 season. Develop a regression model to...

  • 5. You and your friends want to select a few Major League Baseball (MLB) teams to...

    5. You and your friends want to select a few Major League Baseball (MLB) teams to create a fantasy league. You write a code to select teams at random. (Divisions and teams are listed below.) (12pts.) AL East AL West AL Central HAstros Orioles White Sox B C Red Sox Indians Angels Tigers Yankees Athletics KC Rays Royals Mariners T Twins Blue Jays Rangers NL East NL Central NL West C A Diamondbacks Braves Cubs Marlins Reds Rockies Mets Brewers...

  • Team League Wins ERA BA HR SB Errors Built Size Attendance Payroll Pittsburgh Pirates NL 57...

    Team League Wins ERA BA HR SB Errors Built Size Attendance Payroll Pittsburgh Pirates NL 57 5.00 0.242 126 87 127 2001 38496 1.61 34.9 San Diego Padres NL 90 3.39 0.246 132 124 72 2004 42445 2.13 37.8 Oakland Athletics AL 81 3.56 0.256 109 156 99 1966 34077 1.42 51.7 Texas Rangers AL 90 3.93 0.276 162 123 105 1994 49115 2.51 55.3 Florida Marlins NL 80 4.08 0.254 152 92 123 1987 36331 1.54 55.6 Arizona Diamondbacks...

  • ****PLEASE CODE IN C++ and line doc explaining what is happening!***** **HERE IS THE INPUT FILE(.txt)*****...

    ****PLEASE CODE IN C++ and line doc explaining what is happening!***** **HERE IS THE INPUT FILE(.txt)***** Ottawa Senators New York Rangers Boston Bruins Montreal Canadiens Montreal Canadiens Toronto Maple Leafs New York Rangers Chicago Blackhawks Montreal Maroons Detroit Red Wings Detroit Red Wings Chicago Blackhawks Boston Bruins New York Rangers Boston Bruins Toronto Maple Leafs Detroit Red Wings Montreal Canadiens Toronto Maple Leafs Montreal Canadiens Toronto Maple Leafs Toronto Maple Leafs Toronto Maple Leafs Detroit Red Wings Toronto Maple Leafs...

  • The average number of minutes Americans commute to work is 27.7 minutes (Sterling's Best Places, April...

    The average number of minutes Americans commute to work is 27.7 minutes (Sterling's Best Places, April 13, 2012). The average commute time in minutes for 48 cities are as follows: Click on the datafile logo to reference the data. DATA file Albuquerque Atlanta Austin Baltimore Boston Charlotte Chicago Cincinnati Cleveland Columbus Dallas Denver Detroit El Paso Fresno Indianapolis 23.3 28.3 24.6 32.1 31.7 25.8 38.1 24.9 26.8 23.4 28.5 28.1 29.3 24.4 23.0 24.8 Jacksonville Kansas City Las Vegas Little...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT