Question

Chapter 05 Case study Baseball team For this case study, you’ll use the programming skills that...

Chapter 05 Case study Baseball team

For this case study, you’ll use the programming skills that you learn in Murach’s Python Programming to develop a program that helps a person manage a baseball team. This program stores the data for each player on the team, and it also lets the manager set a starting lineup for each game. After you read chapter 2, you can develop a simple program that calculates the batting average for a player. Then, after you complete most of the other chapters, you can enhance and improve this program.

Assignment: Test and debug the program

Specifications

  • Create a list of valid entries and the correct results for each set of entries. Then, make sure that the results are correct when you test with these entries.
  • Create a list of invalid entries. These should include entries that test the limits of the allowable values. Then, handle the invalid integers (such as negative integers and unreasonably large integers). In addition, make sure the user can’t enter data that doesn’t make sense (such as a player having more hits than at bats).
  • Don’t attempt to handle invalid entries that cause exceptions, such as the user entering a string like “x” for an integer value. You can do that after you read chapter 8.
  • Please include a screenshot of testing and debugging
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#------------- Start of baseball.py -----------------
#baseball.py
#class which stores player details
class Player:
   #constructor
   def __init__(self,name,pos,age,g,ab,r,h):
       #set values if all the values are valid
       res,errs = self.validate(name,pos,age,g,ab,r,h)
       if(res):
           self.name = name
           self.pos = pos
           self.age = age
           self.g = g
           self.ab = ab
           self.r = r
           self.h = h
       else:
           #if not print errors got.
           #set name as - for detecting invalidity
           print("Error(s) Occured: ")
           print(errs)
           print("Failed to Insert\n")
           self.name = "-"
   #function to validate player ffields
   def validate(self,name,pos,age,g,ab,r,h):
       errors = ""
      
       if(not self.validate_pos(pos)):
           errors += "Invalid Position, Positions: P, C, 1B, 2B, 3B, SS, LF, CF, RF, OF, DH, PH, PR\n"
       if(age <= 0 or age >=100):
           errors += "Invalid Age, Age must lies between 1 to 100\n"
       if(g<0 or g > 10000):
           errors += "Invalid Number Of Games Played, It must be 1 to 10000\n"
       if(ab < 0 or ab > 100000):
           errors += "Invalid At Bats\n"
       if(r < 0):
           errors += "Invalid runs\n"
       if(h<0 or h >ab):
           errors += "Invalid Hits\n"
       if(errors == ""):
           return True,""
       else:
           return False,errors
   #function to validate player position
   def validate_pos(self,pos):
       positions = ["P","C","1B","2B","3B","SS","LF","CF","RF","OF","DH","PH","PR"]
       if(pos.upper() in pos):
           return True
       else:
           return False
   #function to cal batting average
   def cal_bat_ave(self):
       return self.h/self.ab
   #function to print player data
   def print_player(self):
       res = "{:18} {:5} {} {:5} {:5} {:5} {:5} {:5.2f}\n"
       print(res.format(self.name,self.pos,self.age,self.g,self.ab,self.r,self.h,self.cal_bat_ave()))
#function that loads the input file
def load_csv(file_name):
   f = open(file_name,"r")
   line = f.readline()
   cols = line.strip().split(",")
   line = f.readline()
   players = []
   while(line):
       pos,name,age,g,ab,r,h = line.strip().split(",")
       #convert to int because data read from text file
       #will be in string format
       age = int(age)
       g = int(g)
       ab = int(ab)
       r = int(r)
       h = int(h)
       player = Player(name,pos,age,g,ab,r,h)
       #if not valid add players to array
       if(player.name != "-"):
           players.append(player)
           print("Successfully Inserted Player:",player.name)
       line = f.readline()
   return players
#function to ask input for player details
#and append them to passed array
def add_player(players):
   name = input("Enter Name : ").strip()
   pos = input("Enter Pos : ").strip()
   age = int(input("Enter Age :").strip())
   g = int(input("Enter No.of Games Played :").strip())
   ab = int(input("Enter No.of At Bats :").strip())
   r = int(input("Enter No.of Runs :").strip())
   h = int(input("Enter No.of Hits :").strip())
   player = Player(name,pos,age,g,ab,r,h)
   if(player.name != "-"):
       players.append(player)
#function to print statistics
def print_stats(players):
   print("\n************************ PLAYERS ********************************\n")
   print("\nName \t\t Pos Age \t G \tAB \tR \tH BA\n")
   for i in range(len(players)):
       players[i].print_player()
   print("\n**********************************************************************");
players = load_csv("baseball.csv")
print_stats(players)

while(True):
   print("1. Add Player")
   print("2. Print Players")
   print("3. Exit")
   ch = int(input("Enter your Choice: "))
   if(ch == 1):
       add_player(players)
   elif(ch == 2):
       print_stats(players)
   elif(ch == 3):
       print("Exiting...")
       break
   else:
       print("Invalid Input")
#------------- End of baseball.py -----------------

------------------------- Input file: baseball.csv --------------------

Pos,Name,Age,G,AB,R,H
C,Josh Phegley,31,69,201,31,52
1B,Matt Olson,25,54,199,30,49
2B,Jurickson Profar,26,78,285,33,62
SS,Marcus Semien,28,88,360,57,97
3B,Matt Chapman,26,86,328,57,87
LF,Robbie Grossman,29,72,228,33,58
CF,Ramon Laureano,24,87,312,48,82
RF,Stephen Piscotty,28,76,297,37,72
DH,Khris Davis,31,70,261,36,63
UT,Chad Pinder,27,61,183,27,46
UT,Mark Canha,30,55,152,35,36
1B,Kendrys Morales,36,34,108,9,22
C,Nick Hundley,35,31,70,5,14
C,Beau Taylor,29,8,19,3,4
2B,Franklin Barreto,23,4,13,2,1
OF,Skye Bolt,25,4,7,1,1
C,Chris Herrmann,31,2,4,1,1
P,Chris Bassitt,30,2,3,0,0
P,Brett Anderson,31,1,3,0,2
P,Daniel Mengden,26,1,2,0,0
P,Frankie Montas,26,1,1,0,0
P,Yusmeiro Petit,34,4,1,0,0
P,Tanner Anderson,26,1,1,0,0

---------------------------- Sample Execution ---------------------


Format the code baseball.py as it is in the images of code posted.

there are only 118 lines of code. copy and paste the code part only . then make indentation of each statement like iin the image and execute.

save the baseball.csv file in the same location baseball.py.

Add a comment
Know the answer?
Add Answer to:
Chapter 05 Case study Baseball team For this case study, you’ll use the programming skills that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question: Case Study Baseball Team Manager For this case study, you’ll use the programming skills that...

    Question: Case Study Baseball Team Manager For this case study, you’ll use the programming skills that you ... Case Study Baseball Team Manager For this case study, you'll use the programming skills that you learn in Murach s Python Programming to develop a program that helps a person manage a baseball team. This program stores the data for each player on the team, and it also lets the manager set a starting lineup for each game After you read chapter...

  • Chapter 8 Python Case study Baseball Team Manager For this case study, you’ll use the programming...

    Chapter 8 Python Case study Baseball Team Manager For this case study, you’ll use the programming skills that you learn in Murach’s Python Programming to develop a program that helps a person manage a baseball team. This program stores the data for each player on the team, and it also lets the manager set a starting lineup for each game. After you read chapter 2, you can develop a simple program that calculates the batting average for a player. Then,...

  • Can you please Test and debug the program below using the following specifications? Many thanks! Create...

    Can you please Test and debug the program below using the following specifications? Many thanks! Create a list of valid entries and the correct results for each set of entries. Then, make sure that the results are correct when you test with these entries. Create a list of invalid entries. These should include entries that test the limits of the allowable values. Then, handle the invalid integers (such as negative integers and unreasonably large integers). In addition, make sure the...

  • Case Study Baseball Team Manager. CMSC 135 Python Chapter 7 Assignment: Use a file to save...

    Case Study Baseball Team Manager. CMSC 135 Python Chapter 7 Assignment: Use a file to save the data Update the program so it reads the player data from a file when the program starts andwrites the player data to a file anytime the data is changed What needs to be updated: 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...

  • Week 10 Python programming Baseball Team Manager MENU OPTIONS 1 – Display lineup 2 – Add...

    Week 10 Python programming 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 POS AB H AVG Player 1 Denard Span CF 545 174 0.319 2 Brandon Belt 1B 533 127 0.238 3 Buster Posey C 535 176 0.329 4 Hunter...

  • c++ A menu-driven program gives the user the option to find statistics about different baseball players....

    c++ A menu-driven program gives the user the option to find statistics about different baseball players. The program reads from a file and stores the data for ten baseball players, including player’s team, name of player, number of homeruns, batting average, and runs batted in. (You can make up your data, or get it online, for example: http://espn.go.com/mlb/statistics ) Write a program that declares a struct to store the data for a player. Declare an array of 10 components to...

  • As we continue with our study of programming fundamentals, here is a short extra credit programming...

    As we continue with our study of programming fundamentals, here is a short extra credit programming challenge involving selection control structures. To be specific, the program specifications below and the algorithm you develop and write will involve the set-up and use of either nested if-else statements and/or switch statements. Choose one of the following programming challenges below for this algorithm workbench extra credit programming challenge… ------------------------------------------------------------------------------------------- Finding median Use selection control structures to write a C++ program that determines the...

  • In Java Problem Description/Purpose:  Write a program that will compute and display the final score...

    In Java Problem Description/Purpose:  Write a program that will compute and display the final score of two teams in a baseball game. The number of innings in a baseball game is 9.    Your program should read the name of the teams. While the user does not enter the word done for the first team name, your program should read the second team name and then read the number of runs for each Inning for each team, calculate the...

  • I need help programming the main args. I am not sure what to do after I...

    I need help programming the main args. I am not sure what to do after I create an array list and scanner.h import java.util.ArrayList; import java.util.Scanner; public class Fantasy Football public static void main(string] args) ArrayList<String> availablePlayers - new ArrayList<String>0; addPlayers (avallablePlayers), Stringt roster; roster = new String[5]; Scanner player = new Scanner(System.in); System.out.println("Enter Player you would like on your tein: "); String playeri - player.nextLine(); public static int search(ArrayListString> array, String player) for (int i = 0; i <...

  • qbasic is the programming language Chapter 8 Do Programming Exercise 8-3 om page 414. Create a...

    qbasic is the programming language Chapter 8 Do Programming Exercise 8-3 om page 414. Create a string array to hold the months of the year. Call it anything you want to just make sure it's a string array is at the end of the name, e.g. months). Create a DATA statement that holds the month names: DATA "jan","Feb", "Mar" etc. Next you'll need a FOR loop to READ the DATA into the array, Call a SUB to get the rainfall...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT