Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

how do would I solve this problem? I saw a similar one but the code didn\'t seem

ID: 3732130 • Letter: H

Question

how do would I solve this problem? I saw a similar one but the code didn't seem to run properly

World Series Baseball (Points 10)

Given a text file that stores the World Series winners and losers for the years 1903 to 2015, write a program that allows a user do the following: 1 – Find the World Series winner for a particular year 2 – Find the World Series loser for a particular year 3 – Count the number of times a team has won the World Series You can get the text file here: http://www.cs.uri.edu/~cingiser/csc110/labs/python/worldseries.txt Your program should allow the user to continue to enter choices until he/she is finished. To get you started, download this python code: http://www.cs.uri.edu/~cingiser/csc110/labs/python/worldseries_skeleton.py

Part A: Run the program (1 point) Save the Python file and the text file both to the same location on the computer. Run the program in IDLE to see how it should work. You will notice that it runs, but does not do anything except execute the main function and the getChoice function.

Part B: Get the data (3 points) Write the function to get the data from the data file. The function is called: getChamps Be sure to look at the file to see how the data is organized so that you can read it and store it into three separate lists. Also make sure that you account for the user entering an incorrect file name. You may need another function to handle this possible error. Insert a call into the main function in the appropriate place.

Part C: Find the winner and loser (2 points) Write the function to find the winner and loser of the World Series for a given year. The function is called: findWinnerAndLoser The function should take as a parameter the year for which they would like to know the World Series results. It should then look through the yearList to find the given year and return the winner and loser from that year. The program should display an error if the user enters a year that is not in the list or if the user enters something other than a year.

Part D: Count the wins (2 points) Write the function to count the number of wins for a given team. The function is called: countWins The function should take as a parameter the name of a team, and then count how many times that team was the winner of the World Series. The function should return the number of wins for that team. Add the function call to the main function in the appropriate place.

Part E: Check for Errors (2 points) Modify the getChoice function so that it handles the case where a user enters input other than an integer (use exception handling).

Explanation / Answer

# World Series Results

# Given a file with the World Series Champions since 1904
# Allow user to ask various questions about the results

def getChamps():
# This function will get the data from the data file - be sure to look at the format of the data in the
# file and read each line as we did with the phone search program in class.
# The function should return the list of years, the list of winners and the list of losers
#we have taken 4 lists first list is a dummy list to store data
readlist=[]
#this list contains all the years
yearlist=[]
#this list contains all the winners
winnerlist=[]
#this list contains all the losers
loserlist=[]
#change the file path here
#with the open function we read the file line by line
with open("C:/Users/DAD-PC/Desktop/worldseries.txt") as fp:
#for each line in file we split it based on ',' and store in list
for line in fp:
readlist.append(line.split(","))
#for each value in dummylist we fetch the year,winner,loser and append the respective lists
for l in readlist:
yearlist.append(int(l[0]))
winnerlist.append(l[1])
loserlist.append(l[2][:-2])
  
  
  
print("")
print("This is where you will get the data from the data file")
print("")
# Make sure to return the correct information from this function
return yearlist,winnerlist,loserlist

def getChoice():
# This function displays the menu of choices for the user
# It reads in the user's choice and returns it as an integer
print("")
print("Make a selection from the following choices:")
print("1 - Find the World Series Winner and Loser for a particular year")
print("2 - Count how many times a team has won the World Series")
print("3 - Find the team that has won the most World Series Championships")
print("4 - Find the team that has lost the most World Series Championships")
print("5 - Quit")
choice = int(input("Enter your choice --> "))
print("")
return choice

def findWinnerAndLoser(year):
#this funciton will accept 1 parameter year
#this function will return the winner and loser of a particular year
#we used exception handler
try:
if(year in yearlist):
#we used the .index function to get the winner and loser as index will be corresponding in the lists.
return winnerlist[yearlist.index(year)],loserlist[yearlist.index(year)]
else:
print("year not found")
except:
print("please enter a valid year")
  
def countWins(team):
#this function will get the 1 parameter team
#this will return the win count for a team
try:
#try except is used for exception handling
#we used .count funciton to find how many times a team has won
if(team in winnerlist):
return winnerlist.count(team)
else:
print("team not found in winner list")
except:
print("please enter a valid team name")
#extra function for choice 3
# def teamLostMostMatches():
# return max(set(loserlist), key=loserlist.count),loserlist.count(max(set(loserlist), key=loserlist.count))
#extra function for choice 4
# def teamWinMostMatches():
# return max(set(winnerlist), key=winnerlist.count),winnerlist.count(max(set(winnerlist), key=winnerlist.count))   
  

def main():
#declared global variables so that we can access in another functions
global yearlist
global winnerlist
global loserlist
# Call the function to get the data from the data file and store the results in three lists
yearlist,winnerlist,loserlist=getChamps()
  
choice = getChoice()
print(choice)
try:
while choice != 5:
if choice == 1:
year = input("Enter the year to search for: ")
print(year)
# Call the function to get the winner and the loser
winner,loser=findWinnerAndLoser(year)
print("The winner for ",year, " was ",winner," .")
print("The loser for ", year, "was ",loser," .")
choice = getChoice()
elif choice == 2:
#if using input() give input in as 'Detroit Tigers'
#use use the function raw_input()
team = input("Enter the team name: ")
# Call the function to get the number of wins for the team
winCount=countWins(team)
print(team, " won the World Series ",winCount," times.")
choice = getChoice()
elif choice == 3:
# Call the function to find the team that won the most championships
#function call for choice 3
#mostWinningTeam,count=teamWinMostMatches()
print("XXX won the World Series the most at XXX times.")
# print(mostWinningTeam," won the World Series the most at",count," times.")
choice = getChoice()
elif choice == 4:
# Call the function to find the team that lost the most championships
#function call for choice 4
# mostLosingTeam,count=teamLostMostMatches()
# print(mostLosingTeam," lost the World Series the most at",count," times.")
print("XXX lost the World Series the most at XXX times.")
choice = getChoice()
else:
print("Error in your choice")
choice = getChoice()
except:
print("please enter a valid integer between 1 to 5")
print("Good-bye")
main()