68.2k views
1 vote
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.

1 Answer

1 vote

Answer:

Check the explanation

Step-by-step explanation:

with open('stats. txt', 'r') as # open the file in read mode

list1 = [] # initialize an empty list

for i in . 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("\\")

Kindly check the attached images below to get the Code Screenshot and Code Output.

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-example-1
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-example-2
User Jameshales
by
5.9k points