The purpose of this assignment is for students to become familiar with writing s
ID: 3698159 • Letter: T
Question
The purpose of this assignment is for students to become familiar with writing simple Python scripts, to use built-in containers (data structures), and to develop a "user friendly" interface utilizing Python's input / output capabilities.
You will write a Python program that reads in Baseball statistics for players on various teams from a CSV (comma separated value) file and display statistical information for a specified player. You will allow the user to find a player and display his statistics.
The interface
The interface (interaction with the user) will be via the terminal (stdin, stdout). The user interface will have 3 stages:
List, in alphabetical order, all teams represented within the file.
Allow the user to choose (menu style) a team, and then display all players for that team
Allow the user to choose (menu style) a player and then display the full set of statistics for that player
You will, after some input (you decide), bring the user back to the main (team list) menu, which will also have a "quit" option.
When you list out the teams or players, enumerate them, so that the user can choose by entering a number; don't make the user type out text (team or player names), etc.
Input
Your program will read a file called sample.in , which contains the player statistics.
The following fields are included for each record, as follows:
player's last name
player's first name
player's team
player's position
number of at bats
number of base hits
number of doubles
number of triples
number of home runs
number of runs batted in (RBI)
batting average (3 decimal places, no leading zero)
Note that all fields within a record are separated by commas, and that no field actually contains a comma.
Please review some of the Python examples from week 4 which demonstrate how to retrieve files (either entire file or line by line) into Python. Also, these examples demonstrate how to parse the line items into individual fields.
Notes, the data structures
You will want to keep the keys (for both team and player lists) in a sorted list for displaying a menu (easy access). For storing statistical details, associating them with a player, associating players with teams, please think about how you would store this. Nice tables will drive a lot of the rest of your code.
You will probably choose to utilize some combination of Python's list and dictionary types (list of dictionaries, dictionaries of lists, ....)
You may parse the input file only once, so you need to store the information for easy retrieval.
sample.in file
Alou,Moises,Mets,OF,328,112,19,1,13,49,.341 Hermedia,Jeremy,Marlins,OF,429,127,31,1,18,63,.296 Howard,Ryan,Phillies,1b,529,142,26,0,47,136,.268 Jacobs,Mike,Marlins,1b,426,113,27,2,17,54,.265 Johnson,Kelly,Braves,2b,521,144,26,0,16,68,.276 Kearns,Austin,Nationals,OF,587,156,35,1,16,74,.266 Renteria,Edgar,Braves,SS,494,164,30,1,12,57,.332 Reyes,Jose,Mets,SS,681,191,36,12,12,57,.280 Rollins,Jimmy,Phillies,SS,716,212,38,20,30,94,.296 Utley,Chase,Phillies,2b,530,176,48,5,22,103,.332 Victorino,Shane,Phillies,OF,456,128,23,3,12,46,.281 Young,Dmitri,Nationals,1b,460,147,38,1,13,74,.320 Zimmerman,Ryan,Nationals,3b,653,174,45,3,24,91,.266
Explanation / Answer
getPlayerStatistics.py
#!/usr/bin/python
# opens user-provided file, or uses sample.in as the default if one is not
# provided
import sys
if len( sys.argv ) < 2 :
fileName = "sample.in"
else :
fileName = sys.argv[1]
file = open(fileName, "r")
wholeTable = {} # stores all data, keys are team names
teamList = [] # the initial list, of just team names
# reads data from file, creating a local list to make writing to wholeTable
# easier. At the end of this block, wholeTable is filled with all data and
# team List is filled with all team names
for i in file :
i = i.strip()
fieldCount = 0
localList = []
for field in i.split(",") :
if (fieldCount == 2) :
key = field
if not (wholeTable.has_key(key)) :
wholeTable[key] = key
wholeTable[key] = []
teamList.append(key)
else :
localList.append(field)
fieldCount += 1
wholeTable[key].append(localList)
# sorts the teamList and displays it with numbered options
teamList.sort()
again = True # boolean controls the continual looping of menu code until
# the quit option is selected
while again :
numberer = 1
print " "
for i in teamList :
print numberer, i
numberer += 1
print " 99 Quit"
# user enters team number, causing the program to look up that team in
# the wholeTable. The players with that key are then listed out. Two
# new playerLists are maintained, one to hold just the last name for
# further lookup, and one to hold first and last name for output purposes
selectedTeamNum = int(raw_input( " Enter the number of the team you want: "
))
if (selectedTeamNum == 99) :
again = False
sys.exit
else :
selectedTeamName = teamList[selectedTeamNum-1]
print selectedTeamName, " "
playerList = []
printPlayerList = []
teamLength = len(wholeTable[selectedTeamName])
for i in range(teamLength) :
player = wholeTable[selectedTeamName] [i] [0]
printPlayer = (wholeTable[selectedTeamName] [i] [1] + " " +
wholeTable[selectedTeamName] [i] [0])
playerList.append(player)
printPlayerList.append(printPlayer)
playersLength = len(playerList)
for i in range(playersLength) :
print i+1, printPlayerList[i]
# user enters player number, causing the program to find that player
# in wholeTable and print the rest of their statistics
selectedPlayerNum = int(raw_input( " Enter the number of the player you" + " want: " ))
selectedPlayerName = playerList[selectedPlayerNum-1]
printSelectedPlayerName = printPlayerList[selectedPlayerNum-1]
print printSelectedPlayerName, " "
for i in range(teamLength) :
if (wholeTable[selectedTeamName] [i] [0] == selectedPlayerName) :
stats = wholeTable[selectedTeamName][i]
print "Position:", stats[2]
print "At Bats:", stats[3]
print "Base Hits:", stats[4]
print "Doubles:", stats[5]
print "Triples:", stats[6]
print "Home Runs:", stats[7]
print "RBIs:", stats[8]
print "Batting Average:", stats[9]
# this line just waits for user input before looping to return to the
# team menu
menuReturn = int(raw_input( " Enter 99 to return to Teams list: "))
sample.in
Alou,Moises,Mets,OF,328,112,19,1,13,49,.341
Hermedia,Jeremy,Marlins,OF,429,127,31,1,18,63,.296
Howard,Ryan,Phillies,1b,529,142,26,0,47,136,.268
Jacobs,Mike,Marlins,1b,426,113,27,2,17,54,.265
Johnson,Kelly,Braves,2b,521,144,26,0,16,68,.276
Kearns,Austin,Nationals,OF,587,156,35,1,16,74,.266
Renteria,Edgar,Braves,SS,494,164,30,1,12,57,.332
Reyes,Jose,Mets,SS,681,191,36,12,12,57,.280
Rollins,Jimmy,Phillies,SS,716,212,38,20,30,94,.296
Utley,Chase,Phillies,2b,530,176,48,5,22,103,.332
Victorino,Shane,Phillies,OF,456,128,23,3,12,46,.281
Young,Dmitri,Nationals,1b,460,147,38,1,13,74,.320
Zimmerman,Ryan,Nationals,3b,653,174,45,3,24,91,.266
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.