In: Computer Science
Chapter 6: Use a list to store the players
Update the program in python so that it allows you to store the players for the starting lineup. This
should include the player’s name, position, at bats, and hits. In addition, the program
should calculate the player’s batting average from at bats and hits.
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: 2
Name: Mike
Position: C
At bats: 0
Hits: 0
Mike was added.
Menu option: 1
Player POS AB H AVG
----------------------------------------------------------------
1 Joe P 10 2 0.2
2 Tom SS 11 4 0.364
3 Ben 3B 0 0 0.0
4 Mike C 0 0 0.0
Menu option: 6
Lineup number: 4
You selected Mike AB=0 H=0
At bats: 4
Hits: 1
Mike was updated.
Menu option: 4
Current lineup number: 4
Mike was selected.
New lineup number: 1
Mike was moved.
Menu option: 7
Bye!
Specifications
Use a list of lists to store each player in the lineup.
Use a tuple to store all valid positions (C, 1B, 2B, etc).
Make sure that the user’s position entries are valid.
Raw code:
pos=('C', '1B', '2B', '3B', 'SS', 'LF', 'CF', 'RF', 'P')
def display(list):
# f1.display1(list)
print("\tPlayer\tPOS\tAB\tH\tAVG")
j=1
for i in range(len(list)):
if int(list[i][2])==0:
avg=0
else:
avg=round(int(list[i][3])/int(list[i][2]),3)
print(str(j)+'\t'+str(list[i][0])+'\t'+str(list[i][1])+'\t'+str(list[i][2])+'\t'+str(list[i][3])+'\t'+str(avg))
j=j+1
def add1(list):
name=input("Name: ")
p=input("Position:")
while p not in pos:
print("Wrong position, Please enter valid position")
p=input("Position: ")
at=input("At bats: ")
hit=input("Hits: ")
l=[]
l.append(name)
l.append(p)
l.append(at)
l.append(hit)
list.append(l)
print(name,' was added. ')
def remove1(list):
n=int(input("Enter the lineup number to remove: "))
print(list[n-1][0]+' was deleted.')
del list[n-1]
def move(list):
n1=int(input("Enter a current lineup number to move: "))
print(list[n1-1][0]+' was selected. ')
n2=int(input("Enter a new lineup number: "))
print(list[n1-1][0]+' was moved. ')
l=list[n1-1]
list.insert(n2-1,1)
del list[n1]
def editpos(list):
n=int(input("Enter a lineup number to edit: "))
print("You selected " +list[n-1][0]+" POS="+list[n-1][1])
p=input("Enter a new Position: ")
while p not in pos:
print("Wrong position, Please enter valid postion")
p=input("Enter a new Position: ")
list[n-1][1]=p
print(list[n-1][0]+' was updated.')
def editstat(list):
n=int(input("Enter a lineup number to edit: "))
print('You selected ',list[n-1][0])
at=input("Enter new At bats: ")
hit=input("Enter new hits: ")
list[n-1][2]=at
list[n-1][3]=hit
print(list[n-1][0]+' was updated')
def main():
# list=f1.read()
list=[['Joe','P',10,2,0.2],['Tom','SS',11,4,0.364],['Ben','3B',0,0,0.0],['Mike','C',0,0,0.0]]
print("="*64)
print("MENU OPTIONS")
print("1 - Display 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("\nPOSITIONS")
print(pos)
print("="*64)
ch=input("\nMenu option: ")
while ch<='7':
if ch=='1':
display(list)
elif ch=='2':
add1(list)
elif ch=='3':
remove1(list)
elif ch=='4':
move(list)
elif ch=='5':
editpos(list)
elif ch=='6':
editstat(list)
elif ch=='7':
print('Bye!')
exit()
ch=input('\nMenu option: ')
if __name__=="__main__":
main()
Editor;
output:
Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.
"Please refer to the screenshot of the code to understand the indentation of the code".
Thank you! Do upvote.