Eile Edit view Hbtory Bookmarks bols Help a sakaieURIcsc 110
Solution
# World Series Results
# Given a file with the World Series Champions since 1904
# Allow user to ask various questions about the results
win = []
yr = []
lsr = []
def findWinnerAndLoser(year):
try:
year = int(year)
except ValueError:
print(\"Please enter an integer\")
return -1,-1
if year in yr:
pl = yr.index(year)
return win[pl],lsr[pl]
else:
print(\"We do not have the data for that year.\")
return -1,-1
def countWins(team):
count = 0
for l in win:
if l == team:
count+=1
return count
def countLosses(team):
count = 0
for l in lsr:
if l == team:
count+=1
return count
def greatestWins():
mx=0
team = \"\"
for l in win:
if countWins(l) > mx:
mx = countWins(l)
team = l
return mx,team
def greatestLosses():
mx=0
team = \"\"
for l in lsr:
if countWins(l) > mx:
mx = countLosses(l)
team = l
return mx,team
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
f = open(\"worldseries.txt\",\'r\')
for line in f.readlines():
line = line[0:-1]
line = line.split(\',\')
yr.append(int(line[0]))
win.append(line[1])
lsr.append(line[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
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\")
try:
choice = int(input(\"Enter your choice --> \"))
except ValueError:
print(\"Please enter an integer\")
return -1
print(\"\")
return choice
def main():
# Call the function to get the data from the data file and store the results in three lists
getChamps()
choice = getChoice()
while choice != 5:
if choice == 1:
year = input(\"Enter the year to search for: \")
# Call the function to get the winner and the loser
win,loser = findWinnerAndLoser(year)
if(win==-1):
choice = getChoice()
continue
print(\"The winner for \"+str(year)+\" was \"+win)
print(\"The loser for \"+str(year)+\" was \"+loser)
choice = getChoice()
elif choice == 2:
team = input(\"Enter the team name: \")
# Call the function to get the number of wins for the team
print(team+\" won the World Series \"+str(countWins(team))+\" times.\")
choice = getChoice()
elif choice == 3:
# Call the function to find the team that won the most championships
mx,team = greatestWins()
print(team+\" won the World Series the most at \"+str(mx)+\" times.\")
choice = getChoice()
elif choice == 4:
# Call the function to find the team that lost the most championships
mx,team = greatestLosses()
print(team+\" lost the World Series the most at \"+str(mx)+\" times.\")
choice = getChoice()
else:
print(\"Error in your choice\")
choice = getChoice()
print(\"Good-bye\")
main()


