Starting from:
$30

$24

Assignment 4: Baseball Fun With Classes Solution

Part 2




Your first task is to create a function with one parameter (a file name) that reads each line of data and creates a PlayersYear object from that line, then places it in a list. After all lines are read, this list of PlayersYear objects is returned to the main function.



Next, create a function with one parameter (the list of PlayersYear objects) to display all the players in a table proceeded by heading1.



Next, create a function with one parameter (the list of PlayersYear objects) to display the averages of each player in a table proceeded by heading2.



Next, create a function to get the key that permits sorting on the number of homeruns a player hits. This function has one parameter, (a Player object). This function returns the number of home runs hit by this PlayersYear object.



Next, create a function with one parameter (the list of PlayersYear objects) that sorts this list with respect to home runs.



Next, create a function to get the key that permits sorting on the number of at bats. This function has one parameter, (a Player object). This function returns the number of at bats of this PlayersYear object.



Next, create a function with one parameter (the list of PlayersYear objects) that sorts this list with respect to at bats.



Finally, the main() function (is where the functions are called) is described by the comments below:



Ask user to enter file's name



Read the data from the file, return a list of PlayersYear objects



Display all stats of the players (heading1)



Display all averages (heading2)



Display all stats of the players sorted by homer



(heading1)




Display all stats of the players sorted by at bats (heading1)



Display all averages sorted from highest to lowest slugging percentage (heading2)



Deliverables:




Zip together, the files PlayersYear.py, assign4.py, baseball.txt, and




dimaggio.txt. Don’t forget to document each function!

Here are some outputs:




All players in file order:




-------------------------------------------------------------------------------------




Player Year AB S D T HR BA SLG

-------------------------------------------------------------------------------------

Hank Aaron
1971
495
90
22
3
47
0.327
0.669
Barry Bonds
2002
403
70
31
2
46
0.370
0.799
Rod Carew
1974
599
180
30
5
3
0.364
0.446
Ty Cobb
1912
553
166
30
23
7
0.409
0.584
Joe DiMaggio
1937
621
119
35
15
46
0.346
0.673
Mickey Mantle
1956
533
109
22
5
52
0.353
0.705
Roger Maris
1961
590
78
16
4
61
0.269
0.620
Willie Mays
1963
596
110
32
7
38
0.314
0.582
Wilbur Stargell
1978
390
67
18
2
28
0.295
0.567
Ted Williams
1957
420
96
28
1
38
0.388
0.731
Babe Ruth
1920
457
73
36
9
54
0.376
0.849
Bill Mazeroski
1960
538
110
21
5
11
0.273
0.392
Frank Gustine
1941
463
93
24
7
1
0.270
0.359
Thurman Munson
1975
597
151
24
3
12
0.318
0.429
Orlando Cepada
1967
563
121
37
0
25
0.325
0.524
All averages in file order:














---------------------------------------------










Player
Slugging %


Year










---------------------------------------------










Joe DiMaggio
0.576


1936










Joe DiMaggio
0.673


1937










Joe DiMaggio
0.527


1938










Joe DiMaggio
0.592


1939










Joe DiMaggio
0.556


1940










Joe DiMaggio
0.559


1941










Joe DiMaggio
0.447


1942










Joe DiMaggio
0.453


1946










Joe DiMaggio
0.464


1947










Joe DiMaggio
0.531


1948










Joe DiMaggio
0.492


1949










Joe DiMaggio
0.507


1950










Joe DiMaggio
0.363


1951













Sorted by home runs:




-------------------------------------------------------------------------------------




Player Year AB S D T HR BA SLG

-------------------------------------------------------------------------------------

Frank Gustine
1941
463
93
24
7
1
0.270
0.359
Rod Carew
1974
599
180
30
5
3
0.364
0.446
Ty Cobb
1912
553
166
30
23
7
0.409
0.584
Bill Mazeroski
1960
538
110
21
5
11
0.273
0.392
Thurman Munson
1975
597
151
24
3
12
0.318
0.429
Orlando Cepada
1967
563
121
37
0
25
0.325
0.524
Wilbur Stargell
1978
390
67
18
2
28
0.295
0.567
Willie Mays
1963
596
110
32
7
38
0.314
0.582
Ted Williams
1957
420
96
28
1
38
0.388
0.731
Barry Bonds
2002
403
70
31
2
46
0.370
0.799
Joe DiMaggio
1937
621
119
35
15
46
0.346
0.673
Hank Aaron
1971
495
90
22
3
47
0.327
0.669
Mickey Mantle
1956
533
109
22
5
52
0.353
0.705
Babe Ruth
1920
457
73
36
9
54
0.376
0.849
Roger Maris
1961
590
78
16
4
61
0.269
0.620
Sorted by slugging percentages (hi to low):










---------------------------------------------










Player
Slugging %


Year










---------------------------------------------










Babe Ruth
0.849


1920










Barry Bonds
0.799


2002










Ted Williams
0.731


1957










Mickey Mantle
0.705


1956










Joe DiMaggio
0.673


1937










Hank Aaron
0.669


1971










Roger Maris
0.620


1961










Ty Cobb
0.584


1912










Willie Mays
0.582


1963










Wilbur Stargell
0.567


1978










Orlando Cepada
0.524


1967










Rod Carew
0.446


1974










Thurman Munson
0.429


1975










Bill Mazeroski
0.392


1960










Frank Gustine
0.359


1941










Sorted by at bats:




-------------------------------------------------------------------------------------




Player Year AB S D T HR BA SLG

-------------------------------------------------------------------------------------

Joe DiMaggio
1949
329
60
14
6
14
0.286
0.492
Joe DiMaggio
1951
482
71
22
4
12
0.226
0.363
Joe DiMaggio
1939
524
108
32
6
30
0.336
0.592
Joe DiMaggio
1946
567
93
20
8
25
0.257
0.453
Joe DiMaggio
1940
572
111
28
9
31
0.313
0.556
Joe DiMaggio
1947
601
107
31
10
20
0.280
0.464
Joe DiMaggio
1950
606
83
33
10
32
0.261
0.507
Joe DiMaggio
1937
621
119
35
15
46
0.346
0.673
Joe DiMaggio
1941
622
109
43
11
30
0.310
0.559
Joe DiMaggio
1936
637
118
44
15
29
0.323
0.576
Joe DiMaggio
1938
660
117
32
13
32
0.294
0.527
Joe DiMaggio
1948
669
114
26
11
39
0.284
0.531
Joe DiMaggio
1942
680
123
29
13
21
0.274
0.447

More products