In: Computer Science
Console
================================================================
Baseball Team Manager
MENU OPTIONS
1 – Display lineup
2 – Add player
3 – Remove player
4 – Move player
5 – Edit player position
6 – Edit player stats
7 - Exit program
POSITIONS
C, 1B, 2B, 3B, SS, LF, CF, RF, P
================================================================
Menu option: 1
Player POS AB H AVG
----------------------------------------------------------------
1 Denard OF 545 174 0.319
2 Joe 2B 475 138 0.291
3 Buster C 535 176 0.329
4 Hunter OF 485 174 0.359
5 Brandon SS 532 125 0.235
6 Eduardo 3B 477 122 0.256
7 Brandon 1B 533 127 0.238
8 Jarrett OF 215 58 0.27
9 Madison SP 103 21 0.204
Menu option: 7
Bye!
Specifications
· Use a CSV file to store the lineup.
· Store the functions for writing and reading the file of players in a separate module
than the rest of the program.
· Make sure that the user’s data entries are valid.
Edit this code and any comments you could put in the CSV parts to help explain how they work or what they do would be a great help!
players = [["Joe", "2B", 10, 2], ["Tom", "SS", 11, 4], ["Ben",
"3B", 0,0]]
pos=('C', '1B', '2B', '3B', 'SS', 'LF', 'CF', 'RF', 'P')
def list_All_Players():
print("#\t" + "Name\t" + "Pos\t" + "Bat\t" + "Hit")
print("------------------------------------------------------------------")
for player in players:
print(str(players.index(player)) + "\t" + player[0]\
+ "\t" + player[1] + "\t" + str(player[2]) +\
"\t" + str(player[3]))
def show_Menue():
print("====================================================================")
print("\t\t\t Baseball Team Manager")
print("MENU OPTIONS")
print("1-Display the lineup")
print("2-Add player")
print("3-Remove player")
print("4-Move player")
print("5-Edit player position")
print("6-Edit player stats")
print("7-Exit program")
print("POSITIONS")
print(pos)
print("====================================================================")
def edit_Player_Position():
playerno = int(input("Enter a player number \t"))
newposition = input("Enter a new position \t")
while newposition not in pos:
newposition = input("Enter a valid position \t")
players[playerno][1] = newposition
def move_A_Player():
playerno = int(input("Enter a player number \t"))
newnum = int(input("Enter a number to move player to \t"))
moved = players.pop(playerno)
players.insert(newnum, moved)
def remove_A_Player():
playerno = int(input("Enter a player number you want to remove
\t"))
players.remove(players[playerno])
def add_New_Player():
name = str(input("Name: "))
position = str(input("Postion: "))
atbats = int(input("At Bats: "))
hits = int(input("Hits: "))
player = []
player.append(name)
player.append(position)
player.append(atbats)
player.append(hits)
players.append(player)
def edit_Player_Stats():
playerno = int(input("Enter a player number \t"))
if choice == 1:
atbat = int(input("Enter new at bat value \t"))
players[playerno][2] = atbat
elif choice ==2:
hits = int(input("Enter a new hit value \t"))
players[playerno][3] = hits
show_Menue()
option = int(input("Menu option \t"))
while option != 7:
if (option == 1):
list_All_Players()
elif (option ==2):
add_New_Player()
elif (option == 3):
remove_A_Player()
elif (option == 4):
move_A_Player()
elif (option == 5):
edit_Player_Position()
elif (option ==6):
edit_Player_Stats()
if (option ==7):
print("Bye")
option = int(input("Menu option\t"))
#main.py
import file as f #importing file which contains read and write
functions of csv file
pos=('C', '1B', '2B', '3B', 'SS', 'LF', 'CF', 'RF', 'P')
#positions
def list_All_Players(): #to display the players list
players=f.read() #read playes from csv file
print(" \t" + "Name\t" + "Pos\t" + "Bat\t" + "Hit\t"+"Avg")
print("------------------------------------------------------------------")
i=1
for player in players:
avg=round((int(player[3]))/int(player[2]),3) #calculating
average
print(str(i)+"\t"+player[0]+"\t"+player[1]+"\t"+str(player[2])+"\t"+str(player[3])+"\t"+str(avg))
i+=1
def show_Menue(): #menu
print("====================================================================")
print("\t\t\t Baseball Team Manager")
print("MENU OPTIONS")
print("1-Display the lineup")
print("2-Add player")
print("3-Remove player")
print("4-Move player")
print("5-Edit player position")
print("6-Edit player stats")
print("7-Exit program")
print("POSITIONS")
print(pos)
print("====================================================================")
def edit_Player_Position(): #edit player position
list=f.read()
playerno = int(input("Enter a player number \t")) #read player
number to edit
newposition = input("Enter a new position \t") #read new
position
while newposition not in pos: #read position until valid
newposition = input("Enter a valid position \t")
list[playerno-1][1] = newposition #change the position
f.write(list) #update the file
def move_A_Player():
list=f.read()
playerno = int(input("Enter a player number \t")) #read
player number to move
newnum = int(input("Enter a number to move player to \t")) #read
moving position
moved = list.pop(playerno-1)
list.insert(newnum-1, moved)
f.write(list) #update the file
def remove_A_Player():
list=f.read()
playerno = int(input("Enter a player number you want to remove
\t")) #read player number to remove
del list[playerno-1] #delete the player
f.write(list) #update the file
def add_New_Player():
players=f.read()
name = str(input("Name: "))
position = str(input("Postion: "))
while position not in pos:
position = input("Enter a valid position \t")
atbats = int(input("At Bats: "))
hits = int(input("Hits: "))
player = []
player.append(name)
player.append(position)
player.append(atbats)
player.append(hits)
players.append(player)
f.write(players) #update the file
def edit_Player_Stats():
players=f.read()
playerno = int(input("Enter a player number \t"))
choice=int(input("Enter choice"))
if choice == 1:
atbat = int(input("Enter new at bat value \t"))
players[playerno-1][2] = atbat
elif choice ==2:
hits = int(input("Enter a new hit value \t"))
players[playerno-1][3] = hits
f.write(players) #update the file
show_Menue()
option = int(input("Menu option \t"))
while option != 7:
if (option == 1):
list_All_Players()
elif (option ==2):
add_New_Player()
elif (option == 3):
remove_A_Player()
elif (option == 4):
move_A_Player()
elif (option == 5):
edit_Player_Position()
elif (option ==6):
edit_Player_Stats()
option = int(input("Menu option\t"))
if (option ==7):
print("Bye")
#file.py
## Function to read data from players.csv file
def read():
list=[]
try:
file=open("players.csv","r") #open file
lines=file.readlines() #read the file
for line in lines:
line=line.strip() #remove extra spaces
l=line.split(",")
list.append(l) #append to list
return list
except FileNotFoundError: #raise filenotfound exception
print("File not found")
## Function that writes data to the players.csv file from the
list
def write(list):
try:
file=open("players.csv", "w") #open file
for line in list:
file.writelines(line[0]+","+line[1]+","+str(line[2])+","+str(line[3])+"\n")
#write to file
except FileNotFoundError:
print("File not found")
#players.csv example file
Dominick,1B,545,174
supriya,C,523,152
Jack,RF,396,125
Alberto,3B,72,19
output: