Answer:
Check the explanation
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("\n")
Kindly check the attached images below to get the Code Screenshot and Code Output.