Question

In: Computer Science

April   107   90   29   31   66   0.344 May   106   94   23   35   72   0.372 June   77  ...

April   107   90   29   31   66   0.344
May   106   94   23   35   72   0.372
June   77   62   12   18   29   0.29
July   115   103   20   34   59   0.33
August   124   102   25   36   63   0.353
September   85   69   20   26   44   0.377

(PYTHON)

Using the attached stats.txt, generate a report of a player’s batting average using hits / at bats by month and slugging percent by dividing total bases by at bats. Print out each month, number hits, number of at bats , BA (batting average) and slugging percent (). The input file is organized by month, plate appearances, at bats, runs, hits and total bases.

Solutions

Expert Solution

with open('stats.txt', 'r') as f:  # open the file in read mode
    list1 = []   # initialize an empty list
    for i in f.readlines():  # read every line of file
        for j in i.split():  # split each line by a space and append it to the list1
            list1.append(j)
    months = []  # initialize required lists to empty list
    plate_apperence = []
    at_bats = []
    runs = []
    hits = []
    total_bats = []

    for i in range(0, len(list1)-1, 7):  # use a for loop and give parameters as start,stop and step count
        months.append(list1[i])          # append list1 elements to required lists
        plate_apperence.append(int(list1[i+1]))
        at_bats.append(int(list1[i+2]))
        runs.append(int(list1[i+3]))
        hits.append(int(list1[i+4]))
        total_bats.append(int(list1[i+5]))
for i in range(len(months)):   # for each element in months
    print("Player's Average Batting and slugging percent for the month ", months[i], " is as shown below")
    print("{:.2f}".format(hits[i]/at_bats[i]))   # calculate Average batting  and slugging percent and print them
    print("{:.2f}".format(total_bats[i]/at_bats[i]))
    print("\n")


Related Solutions

Consider a sample with data values of 26, 24, 23, 18, 31, 35, 29, and 24....
Consider a sample with data values of 26, 24, 23, 18, 31, 35, 29, and 24. Compute the range, interquartile range, variance, and standard deviation
old by years Interval Frequency 35-37 two 32-34 three 29-31 five 26-28 Six 23-25 four 20-22...
old by years Interval Frequency 35-37 two 32-34 three 29-31 five 26-28 Six 23-25 four 20-22 five 17-19 nine 14-16 Sixteen Calculate the above when Passport first used 1- what is the shape of the old distribution? 2- Which accurate statements can describe the relationship between the mean and median old? he mean and median are the same The mean is bigger than the median The mean is less than the median 3- Calculate percentile rank corresponding to the old...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT