import csv # imports function so the program can manipulate a csv file
from operator import itemgetter # imports itemgetter from operator
def open_file():
'''
Paramters: no parameters
Function:
try to ask the user for file name
except FileNotFoundError(file does not exists) keep prompting user until file found
Returns: open file for reading
'''
s = True
while s:
try:
prompt = input("Enter filename: ")
file_obj = open(prompt,'r')
return file_obj
except FileNotFoundError:
print("File not found! Please try again!")
pass
def read_file(fp):
'''
Parameter: takes the file pointer from open_file()
Function: reads the csv and makes a list of list
where each list is each row
Returns: master_list (list of lists)
'''
reader = csv.reader(fp) # attaches a reader to the file fp
next(reader,None) # skips a line, such as a header line
L = []
for i in reader: # line is a list
L.append(i)
return L # temoprary return value so main runs
pass
def shoots_left_right(master_list):
'''
Parameter: master_list (list of lists)
Function: count how many player are left shooter and how many are right shooters
make 2 variables left and right
go through the list and if the player is a left shooter iterate the left variable by 1
otherwise iterate the right variable by 1
Returns: int, int (left, right)
'''
left_shooters = 0
right_shooters = 0
for i in master_list:
if i[1] == "R":
right_shooters += 1
else:
left_shooters += 1
return left_shooters, right_shooters
pass
def position(master_list):
'''
Parameter: master_list (list of lists)
Function: count how many player are left wing, right wing ,center, defense
make 4 variables left_w, right_w, center and defense
go through the list and if the player's position is L then iterate the left_w by 1
if the player's position is R then iterate the right_w by 1
if the player's position is C then iterate the center by 1
if the player's position is D then iterate the defense by 1
Returns: int, int, int, int (left_w, right_w, center, defense)
'''
center = 0
defense = 0
left_wing = 0
right_wing = 0
for i in master_list:
if i[2] == "C":
center += 1
elif i[2] == "D":
defense += 1
elif i[2] == "L":
left_wing += 1
else:
right_wing += 1
return left_wing, right_wing, center, defense
pass # insert your code here
def off_side_shooter(master_list):
'''
Parameter: master_list (list of lists)
Function: count how many player are left wing and shoot right or right wing and shoot left
make 2 variables lw_sr, rw_sl
go through the list and if the player's position is L and shootes right then iterate lw_sr by 1
if the player's position is R and shootes left then iterate rw_ls by 1
Returns: int, int (lw_sr, rw_sl)
'''
left_wing_right = 0
right_wing_left = 0
for i in master_list:
if i[1] == "R" and i[2] == "L":
left_wing_right += 1
elif i[1] == "L" and i[2] == "R":
right_wing_left += 1
return left_wing_right , right_wing_left
pass # insert your code here
def points_per_game(master_list):
'''
Parameter: master_list (list of lists)
Function: make a list l and append a list (points_per_game, player name, position) to l
then sort the list to get the greatest points per game
make a while loop to only take the first 10 element of list l
Returns: list of tuples --> (points_per_game, player name, position)
'''
l = []
f = []
counter = 0
points_per_game = 0
for i in master_list:
points_per_game = float(i[18])
l.append((points_per_game,i[0],i[2]))
l = sorted(l, key = itemgetter(0,1), reverse = True)
while counter < 10:
f.append(tuple(l[counter]))
counter += 1
return f
pass
def games_played(master_list):
'''
Parameter: master_list (list of lists)
Function: make a list l and append a list (games played, player name) to l
sort through the list and remove commas and make games played an integer
then sort the list to get the greatest games played
make a while loop to only take the first 10 element of list l
Returns: list of tuples --> (games played, player name)
'''
l = []
f = []
counter = 0
gp = 0
for i in master_list:
if "," in i[3]:
gp = int(i[3].replace(",",""))
else:
gp = int(i[3])
l.append((gp, i[0]))
l = sorted(l, key = itemgetter(0), reverse = True)
while counter < 10:
f.append(tuple(l[counter]))
counter += 1
return f
pass
def shots_taken(master_list):
'''
Parameter: master_list (list of lists)
Function: make a list l and append a list (shots taken , player name) to l
sort through the list and remove commas and make games played an integer
if -- is then skip the line
then sort the list to get the shots taken
make a while loop to only take the first 10 element of list l
Returns: list of tuples --> (shots taken , player name)
'''
l = []
f = []
counter = 0
st = 0
for i in master_list:
if i[9] == "--":
continue
if "," in i[9]:
st = int(i[9].replace(",",""))
else:
st = int(i[9])
l.append((st, i[0]))
l = sorted(l, key = itemgetter(0), reverse = True)
while counter < 10:
f.append(tuple(l[counter]))
counter += 1
return f
pass
def main():
'''
Calls all the other function
open the file
reads the file
prints the shooting left and right
prints the position left wing, right wing, center, defense
prints the offside shooter --> left wing shooting right and right wing shooting left
use a for loop to go through and print the top 10 players points per game, name , position
use a for loop to go through and print the top 10 players games playes, player name
use a for loop to go through and print the top 10 players shots taken , player name
'''
fp = open_file()
print("\n\n{:^10s}".format("Shooting"))
master_list = read_file(fp)
left, right = shoots_left_right(master_list)
print("left: {:4d}".format(left))
print("right: {:4d}\n".format(right))
print("{:^12s}".format("Position"))
lw, rw, c, d = position(master_list)
print("left: {:4d}".format(lw))
print("right: {:4d}".format(rw))
print("center: {:4d}".format(c))
print("defense: {:4d}\n".format(d))
print("{:^24s}".format("Off-side Shooter"))
lwsr, rwsl = off_side_shooter(master_list)
print("left-wing shooting right: {:4d}".format(lwsr))
print("right-wing shooting left: {:4d}\n".format(rwsl))
points = points_per_game(master_list)
if points:
print("{:^36s}".format("Top Ten Points-Per-Game"))
print("{:<20s}{:>8s}{:>16s}".format("Player","Position","Points Per Game"))
for i in points:
print("{:<20s}{:>8s}{:>16.2f}".format(i[1],i[2],i[0]))
games = games_played(master_list)
if games:
print("\n{:^36s}".format("Top Ten Games-Played"))
print("{:<20s}{:>16s}".format("Player", "Games Played"))
for i in games:
print("{:<20s}{:>16,d}".format(i[1],i[0]))
shots = shots_taken(master_list)
if shots:
print("\n{:^36s}".format("Top Ten Shots-Taken"))
print("{:<20s}{:>16s}".format("Player", "Shots Taken"))
for i in shots:
print("{:<20s}{:>16,d}".format(i[1],i[0]))
pass
if __name__ == "__main__":
main() # call the main function