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

Given a text file that stores the World Series winners and losers for the years

ID: 3543267 • Letter: G

Question

Given a text file that stores the World Series winners and losers for the years 1903 to 2011, write a program that allows the user to 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:


https://www.dropbox.com/s/lyxc9xk8pojavwh/world_series.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:


https://www.dropbox.com/s/2gc8ip9wd5cs9ss/lab_5_startup.py

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():
    yearList = []   
    winnerList = []
    loserList = []
    dummy=[]
    f=open('world_series.txt','r')   #input file is world_series.txt
    for line in f :
        dummy=line.split(',')        #splits the string whenever a comma(,) comes
        yearList.append(dummy[0])    #appends the first string before comma ( in the given format year,winner,loser ) year to yearList
    winnerList.append(dummy[1])    #appends winner to winnerList
    loserList.append(dummy[2])    #appends loser to loserList
    f.close()   
    return yearList, winnerList, loserList

def getChoice():
    print("")
    print("Make a selection from the following choices:")
    print("1 - Find the World Series Winner for a particular year")
    print("2 - Find the World Series Loser for a particular year")
    print("3 - Count how many times a team has won the World Series")
    print("4 - Quit")
    choice = int(input("Enter your choice --> "))
    print("")
    return choice

def findWinner(yearList, winnerList):
    year = raw_input('Please enter the year : ')
    winner = "none"
    flag=0                    #The purpose of flag here is to find whether the input year exists in the yearList or not
    for i in range(len(yearList)):
        if yearList[i]==year :
            winner = winnerList[i]
            flag=1                #flag=1 means the input year exists in the list yearList
        break
    if flag==0 :                #flag=0 means the input year doesnot exist
    print "Error! No Such Year Found"
    return winner


def findLoser(yearList, loserList):
    year = raw_input('Please enter the year : ')
    loser = "none"
    flag=0
    for i in range(len(yearList)) :
        if yearList[i]==year :
            loser = loserList[i]
        flag=1
            break
    if flag==0 :
    print "Error! No Such Year Found"
    return loser

def countWins(winnerList):
    numWins = 0
    winner = raw_input('Please enter the name of the team : ')
    for i in winnerList :
        if i==winner :                #if i == winner then increment the no of wins by 1
        numWins=numWins+1
    return numWins

def main():
    yearList, winnerList, loserList = getChamps()
    choice = getChoice()
    while choice != 4:
        if choice == 1:
            winner = findWinner(yearList, winnerList)
            print "The winner for that year was " + winner
            choice = getChoice()
        elif choice == 2:
            loser = findLoser(yearList, loserList)
        print "The loser for that year was "+ loser
            choice = getChoice()
        elif choice == 3:
            wins = countWins(winnerList)
            print "Your team won the World Series " + str(wins) + " times."
            choice = getChoice()
        else:
            print("Error in your choice")
            choice = getChoice()
    print("Good-bye")
main()                     #calling main function          

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote