Skip to content

Instantly share code, notes, and snippets.

@bholzer
Created April 9, 2014 19:42
Show Gist options
  • Save bholzer/10306989 to your computer and use it in GitHub Desktop.
Save bholzer/10306989 to your computer and use it in GitHub Desktop.
filename = "2009_shots.csv"
def readfile(fname):
'''Read the input file, return a list with all lines from the file'''
f = open(fname, 'r')
file_lines = f.readlines()
f.close
for i in range(0, len(file_lines)):
file_lines[i] = file_lines[i].strip()
return file_lines
#Fields: Team 0, Name 1, Result 2, Type 3, Xpos 4, Ypos 5
def show_pct(lines):
'''Print the teams in alphabetical order and their shooting percentages'''
attempted_shots_dal = 0 #shots attempted by DAL
attempted_shots_sas = 0 #shots attempted by SAS
attempted_shots_hou = 0 #shots attempted by HOU
shots_made_dal = 0 #shots made by DAL
shots_made_sas = 0 #shots made by SAS
shots_made_hou = 0 #shots made by HOU
for i in range(1, len(lines)):
line_list = lines[i].split(',')
if line_list[0] == 'DAL':
if line_list[2] == 'made':
shots_made_dal = shots_made_dal + 1
attempted_shots_dal = attempted_shots_dal + 1
if line_list[0] == 'SAS':
if line_list[2] == 'made':
shots_made_sas = shots_made_sas + 1
attempted_shots_sas = 1 + attempted_shots_sas
if line_list[0] == 'HOU':
if line_list[2] == 'made':
shots_made_hou = shots_made_hou + 1
attempted_shots_hou = 1 + attempted_shots_hou
pctdal = shots_made_dal/attempted_shots_dal
pctsas = shots_made_sas/attempted_shots_sas
pcthou = shots_made_hou/attempted_shots_hou
print('Dallas Mavericks attempted', attempted_shots_dal, 'shots,\
made', shots_made_dal, 'shots and have a', pctdal, 'shooting percentage.')
print('San Antonio Spurs attempted', attempted_shots_sas, 'shots,\
made', shots_made_sas, 'shots and have a', pctsas, 'shooting percentage.')
print('Houston Rockets attempted', attempted_shots_hou, 'shots,\
made', shots_made_hou, 'shots and have a', pcthou, 'shooting percentage.')
return lines
def main():
lines = readfile(filename)
show_pct(lines)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment