78.5k views
2 votes
Chapter 6: Use a list to store the players

Update the program so 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

====:

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: OF
Invalid position. Try again.
POSITIONS
C, 1B, 2B, 3B, SS, LF, CF, RF, P
Position: CF
At bats: 4
Hits: 1
Mike was added.
1
2
3
4
Menu option: 1
Player
Baseball Team Manager
Joe
Tom
Ben
Mike
Hits: 3
Mike was updated.
Menu option: 6
Lineup number: 4
You selected Mike AB=0 H=0
At bats: 10
Menu option: 4
Current lineup number: 4
Mike was selected.
New lineup number: 1
Mike was moved.
Menu option: 7
Bye!
POS
P
SS
3B
с
AB
10
11
9
4
H
2431
==================
AVG
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 entry for position is valid, and entries for hits and at bats
make sense.
0.2
0.364
0.333
0.25

User CudoX
by
8.4k points

1 Answer

6 votes

Answer:

Here's the updated program that allows you to store the players for the starting lineup using a list of lists:

POSITIONS = ('C', '1B', '2B', '3B', 'SS', 'LF', 'CF', 'RF', 'P')

lineup = []

def display_lineup():

print("Player\tPosition\tAt Bats\tHits\tBatting Average")

for player in lineup:

name, position, at_bats, hits = player

if at_bats == 0:

avg = 0

else:

avg = hits / at_bats

print(f"{name}\t{position}\t\t{at_bats}\t{hits}\t{avg:.3f}")

def add_player():

name = input("Name: ")

position = input("Position: ")

if position not in POSITIONS:

print("Invalid position. Try again.")

return

at_bats = int(input("At bats: "))

hits = int(input("Hits: "))

lineup.append([name, position, at_bats, hits])

print(f"{name} was added.")

def remove_player():

name = input("Name: ")

for player in lineup:

if player[0] == name:

lineup.remove(player)

print(f"{name} was removed.")

return

print(f"{name} is not in the lineup.")

def move_player():

name = input("Name: ")

for i, player in enumerate(lineup):

if player[0] == name:

current_index = i

break

else:

print(f"{name} is not in the lineup.")

return

new_index = int(input("New lineup number: ")) - 1

lineup[current_index], lineup[new_index] = lineup[new_index], lineup[current_index]

print(f"{name} was moved.")

def edit_position():

name = input("Name: ")

for player in lineup:

if player[0] == name:

position = input("New position: ")

if position not in POSITIONS:

print("Invalid position. Try again.")

return

player[1] = position

print(f"{name} was updated.")

return

print(f"{name} is not in the lineup.")

def edit_stats():

name = input("Name: ")

for player in lineup:

if player[0] == name:

at_bats = int(input("At bats: "))

hits = int(input("Hits: "))

player[2] = at_bats

player[3] = hits

print(f"{name} was updated.")

return

print(f"{name} is not in the lineup.")

while True:

print("""

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

""")

choice = input("Menu option: ")

if choice == '1':

display_lineup()

elif choice == '2':

add_player()

elif choice == '3':

remove_player()

elif choice == '4':

move_player()

elif choice == '5':

edit_position()

elif choice == '6':

edit_stats()

elif choice == '7':

print("Bye!")

break

else:

print("Invalid option. Try again.")

The program uses a list of lists to store each player in the lineup. Each sublist contains the player's name, position, at bats

Step-by-step explanation:

User Neildt
by
8.2k points