Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Python Here is the initial question: Here is the Source file WorldSeries.txt Boston Americans World Series Not Played in 1904 New York Giants Chicago

In Python

Here is the initial question:

image text in transcribed

Here is the Source file WorldSeries.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 World Series Not Played in 1994 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

Here is the code I have:

image text in transcribed

# Open the WorldSeries.txt file for reading

with open("WorldSeries.txt", "r") as file:

# Create a list of lines from the file

lines = file.readlines()

# Create two dictionaries to store the team names and the winning years

team_wins = {}

year_winners = {}

# Iterate through each line in the file

for line in lines:

# Split the line into year and team

year, team = line.strip().split(" ")

year = int(year)

# Store the winning team for the year in the year_winners dictionary

year_winners[year] = team

# Increment the team's win count in the team_wins dictionary

if team in team_wins:

team_wins[team] += 1

else:

team_wins[team] = 1

# Prompt the user for a year

year = int(input("Enter a year between 1903 and 2009: "))

# Look up the winning team for the year

team = year_winners[year]

# Look up the number of wins for the team

wins = team_wins[team]

# Print the results

print(f"The {team} won the World Series in {year}, and have won {wins} times.")

This is the Error im getting:

year = int(year) ValueError: invalid literal for int() with base 10: 'Boston'

I need help resolving the error with my code to correctly read the file and provide a correct output

7. World Series Winners In this chapter's source code folder, you will find a text file named WorldSeriesWinners.txt. This file 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 the World Series was not played in 1904 or 1994 . There are 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 2009. 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. \# open the Worldseries.txt file for reading with open("Worldseries.txt", "r") as file: \# Create a list of lines from the file lines =file readlines () \# Create two dictionaries to store the team names and the winning years team_wins ={} year_winners ={} \# Iterate through each line in the file for line in lines: \# split the line into year and team year, team = line. strip()split(" " ") year = int ( year ) \# Store the winning team for the year in the year_winners dictionary year_winners [year ]= team \# Increment the team's win count in the team_wins dictionary if team in team wins: team_wins[team ]+=1 else: team_wins [ team ]=1 \# Prompt the user for a year year = int(input("Enter a year between 1903 and 2009: ")) \# Look up the winning team for the year team = year_winners [year ] \# Look up the number of wins for the team wins = team_wins[team ] \# Print the results print( f "The { team } won the world Series in { year }, and have won { wins } times.")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Progress Monitoring Data Tracking Organizer

Authors: Teacher'S Aid Publications

1st Edition

B0B7QCNRJ1

Students also viewed these Databases questions