Question

In: Computer Science

Chapter 6: Use a list to store the players In Python, Update the program so that...

Chapter 6: Use a list to store the players

In Python, Update the program 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.

Solutions

Expert Solution

'''
Python version : 2.7
Python program to create a baseball management system
'''

      
players=[['Joe','P',10,2,0.2],['Tom','SS',11,4,0.364],['Ben','3B',0,0,0.0]] # creating an list of lists with few playes information
position=('C','1B','2B','3B','SS','LF','CF','RF','P') # creating a tuple for storing valid positions
choice=0 # choice variable for taking user's menu choice
lineup=1 # lineup to take the lineup number from user
numOfPlayers=3 # to keep hold on the number of players

print("="*64)
print("\t\tBaseball Team Manager")
print("MENU OPTIONS") # display the menu
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")
# display the positions
print("\nPOSITIONS:")

for i in range(len(position)):
   if i < len(position)-1:
       print(position[i]+","),
   else:
       print(position[i])
  
print("="*64)
while choice!=7: # loop till the choice of user is not equal to 7      
   try:
       choice=int(raw_input("\nMenu Option:")) # take user's menu option in variable choice
       if choice==1: # if choice is 1
           lineup=1
           print("Player POS AB H AVG")
           print("-"*30)
           for player in players: # loop through the list and display each players' information
               print(str(lineup)),
               for item in player:
                   print(str(item)+" "),
               lineup=lineup+1
               print("")
              
       elif choice==2: # if the user's choice is 2
           name=raw_input("Name: ") # take name and position from user
           pos=raw_input("Position: ")
          
           while pos not in position: # if user is provided valid position
               print("Incorrect position")
               pos=raw_input("Position:")
           AB=int(raw_input("At bats: ")) # take at bats and hits value from user
           hit=int(raw_input("Hits: "))      
           if AB!=0: # if at bats is not 0, set avg as hits / at bats
               avg=float(hit)/float(AB) # else set avg as 0
           else:
               avg=0
           NewPlayer=[name,pos,AB,hit,avg] # create a list as newPlayer, and update the list with new info
           players.append(NewPlayer) # append the list newplayer to players list
           numOfPlayers=numOfPlayers+1 # increment number of players by 1
           print(name+' was added')
      
       elif choice==3: # if choice is 3
           lineup=int(raw_input("Lineup number:")) # take lineup number from user
           while lineup>numOfPlayers: # if lineup number is greater than number of player
               print("Please enter the correct lineup number:") # then show appropriate info and again ask for lineup number
               lineup=int(raw_input("Lineup number:")) # display the player's information with specified lineup
           print("You selected " + str(players[lineup-1][0]) + " "+ str(players[lineup-1][1]) +" "+ str(players[lineup-1][2]) +" "+ str(players[lineup-1][3]) +" "+ str(players[lineup-1][4]))
           del players[lineup-1] # delete the player with specified lineup
           numOfPlayers=numOfPlayers-1 # decrement num of players by 1
           print("The player with lineup number " + str(lineup) + " has been removed")
      
       elif choice==4: # if choice is 4
           current_lineup=int(raw_input("Current lineup number:")) # take current lineup from user and print the player's info
          
           while current_lineup <1 or current_lineup>numOfPlayers: # show appropriate message and again ask for lineup if incorrect
               print("Please enter the correct lineup number:") # lineup is provided
               current_lineup=int(raw_input("Current lineup number:"))
           print("You selected " + str(players[current_lineup-1][0]))
          
           new_lineup=int(raw_input("New lineup number:")) # take new lineup from user
           while new_lineup <1 or new_lineup>numOfPlayers: # show appropriate message and again ask for lineup if incorrect
               print("Please enter the correct lineup number:") # lineup is provided
               new_lineup=int(raw_input("New lineup number:"))
              
           (players[current_lineup-1], players[new_lineup-1]) = (players[new_lineup-1], players[current_lineup-1] )
           print(str(players[new_lineup-1][0]) + " was moved")
          

       elif choice==5: # if choice is 5
           lineup=int(raw_input("Lineup number:")) # take line up from user
           while lineup <1 or lineup>numOfPlayers: # show appropriate message and again ask for lineup if incorrect
               print("Please enter the correct lineup number:") # lineup is provided
               lineup=int(raw_input("Lineup number:"))
           print("You selected " + str(players[lineup-1][0]) + " Pos=" + str(players[lineup-1][1])) # show player's information
          
           pos=raw_input("Position:") # Ask the position to change
          
           while pos not in position: # get appropriate position, if incorrect position is specified
               print("Incorrect position")
               pos=raw_input("Position:")
           players[lineup-1][1]=pos # update the player's information with new position
           print(str(players[lineup-1][0]) + " updated")
              
       elif choice==6: # if choice is 6
           lineup=int(raw_input("Lineup number:")) # take correct lineup number from user
           while lineup>numOfPlayers:
               print("Please enter the correct lineup number:")
               lineup=int(raw_input("Lineup number:"))
              
           print("You selected " + str(players[lineup-1][0]) + " AB=" + str(players[lineup-1][2]) + " hits=" + str(players[lineup-1][3]))
           AB=int(raw_input("At bats:")) # take the value of at bats and hits from user
           hit=int(raw_input("Hits:"))
           players[lineup-1][2]=AB # update the player's information with new at bats and hots
           players[lineup-1][3]=hit
           if AB==0: # also calculate average and update
               players[lineup-1][4]=0
           else:
               players[lineup-1][4]=float(hit)/float(AB)
           print(str(players[lineup-1][0]) + " updated")
          
       elif choice==7: # if choice is 7, print break and come out of the loop
           print("Bye!")
          
   except ValueError:
       print('Not a valid option. Please try again')
       print("\nMENU OPTIONS") # display the menu
       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")
      
#end of program      
  
Code Screenshot:

Output:


Related Solutions

Chapter 6: Use a list to store the players Update the program in python so that...
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...
use python 1. Write a program that a. defines a list of countries that are members...
use python 1. Write a program that a. defines a list of countries that are members of BRICS (Brazil, Russia, India, China, Sri Lanka) b. Check whether a country is a member of BRICS or not Program run Enter the name of country: Pakistan Pakistan is not a member of BRICS Enter the name of country : India India is a member of BRICS 2. Write a program to create a list of numbers in the range of 1 to...
programming in python Design a program that initializes a list of 5 items to zero (use...
programming in python Design a program that initializes a list of 5 items to zero (use repetition operator). It then updates that list with a series of 5 random numbers. The program should find and display the following data: -The lowest number in the list - Average of the numbers stored in the list
IN PYTHON: Write a program to study countries and their capitals. The program will store country:capital...
IN PYTHON: Write a program to study countries and their capitals. The program will store country:capital information and allow the user to quiz themselves. These two functions are required for this assignment. You may add other functions that you deem appropriate: - menu()   This function, which displays all the user options to the screen, prompts the user for their choice and returns their choice. This function will verify user input and ALWAYS return a valid choice. - addCapital(dict) This function...
Write a Python program that has a list of 5 numbers [2, 3, 4, 5, 6)....
Write a Python program that has a list of 5 numbers [2, 3, 4, 5, 6). Print the first 3 elements from the list using slice expression. a. Extend this program in a manner that the elements in the list are changed to (6, 9, 12, 15, 18) that means each element is times 3 of the previous value. b. Extend your program to display the min and max value in the list.
Write a program in python such that There exists a list of emails List Ls =...
Write a program in python such that There exists a list of emails List Ls = ['xyz@tz.ac.in','sachin.s@gail.com','avinash.paul@hotmail.com','rohan.d@xyz.ac.in',test@zx.ac.in.co'] Count the number of emails IDS which ends in "ac.in" Write proper program with proper function accepting the argument list of emails and function should print the number of email IDs and also the email IDS ending with ac.in output 2 xyz@tz.ac.in rohan.d@xyz.ac.in ================================= i am trying like this but getting confused len [True for x in Ls if x.endswith('.ac.in')] please write complete...
USE Python 2.7(screen shot program with output) the task is: takes in a list of protein...
USE Python 2.7(screen shot program with output) the task is: takes in a list of protein sequences as input and finds all the transmembrane domains and returns them in a list for each sequence in the list with given nonpolar regions and returns the lists for those. 1. This code should call two other functions that you write: regionProteinFind takes in a protein sequence and should return a list of 10 amino acid windows, if the sequence is less than...
Use Python to Load a file containing a list of words as a python list :param...
Use Python to Load a file containing a list of words as a python list :param str filename: path/name to file to load :rtype: list
for Python 3 Write a python program that Creates a list that has these values in...
for Python 3 Write a python program that Creates a list that has these values in this order, 'Python', 'JavaScript', and 'PHP' Define a displayMyClasses function that sorts the list and then displays each item on the list, one per line, in sorted order. Also, number the displayed list as shown in the "Running a Sample Program" shown below. Define a guessNext function that selects a random element from the list and returns it to the call of the function....
Design and implement a Python program which will allow two players to play the game of...
Design and implement a Python program which will allow two players to play the game of Tic-Tac-Toe in a 4x4 grid! X | O | X | O -------------- O | O | X | O -------------- X | X | O | X -------------- X | X | O | X The rules for this game is the same as the classic, 3x3, game – Each cell can hold one of the following three strings: "X", "O", or "...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT