Question: Help me write this python script The script must read the file 2011-07_sox_games_2.txt in /home/ghoffman/course_files/it116_files and print the results for each game on a single
Help me write this python script
The script must read the file 2011-07_sox_games_2.txt in /home/ghoffman/course_files/it116_files and print the results for each game on a single line. This file has data for Red Sox baseball games spread out over 6 lines. The data appears in the following order
date Red Sox Red Sox score opposing team opposing team score ballpark
You need to create a for loop that prints a single line for each game. This line must have the following information
DATE Red Sox HOME/AWAY OPPOSING_TEAM WIN/LOSE SOX_SCORE - OPPONENT_SCORE
In order to print this line you must define two functions
home_away
win_loss
home_away
This functions must have the following header
def home_away(ballpark):
This is a function that returns "Home" if the ballpark is Fenway Park, otherwise it returns "Away".
win_loss
This functions must have the following header
def win_loss(home_score, opponent_score):
This is a function that returns "Win" if the Red Sox won the game, otherwise it returns "Loss".
Suggestions
Write this script in stages, testing your script at each step
Create the script and add the hashbang line. Open the file 2011-07_sox_games_2.txt for reading. Make the script executable. Run the script. It should produce no output. Fix any errors you find.
Write a for loop that prints each line in the file. Run the script and fix any errors.
Outside the for loop set a line count variable to 0. Inside the loop, before the print statement, increment the line count variable. Change the print statement so it prints the line number as well as the line. Run the script and fix any errors you find.
Comment out the print statement. Write an if statement with a boolean expression which will be true if the remainder, when the line number is divided by 6, is 1. The body of this if statement should simply print the line. Run the script. All you should see is the date of each game. Fix any errors you find.
Remove the print statement. In its place set the variable date equal to the line with the newline character removed. Print the variable date inside the if statement. Run the script and fix any errors you find.
Add an elif clause to the if statement in the for loop. The boolean expression should be true if the remainder, when the line number is divided by 6, is 2. The body of this clause should set the value of the variable home_team to the line with the linefeed character removed. Print this variable inside this elif clause. Run the script and fix any errors you find.
Add another elif clause where the boolean expression will be true if the remainder, when the line number is divided by 6, is 3. Set the value of home_score to the integer value of the line. Print home_score. Run the script and fix any errors you find.
Add another elif clause where the boolean expression will be true if the remainder, when the line number is divided by 6, is 4. Set the value of the variable opponent to the value of the line with the linefeed removed. Print opponent. Run the script and fix any errors you find.
Add another elif clause where the boolean expression will be true if the remainder, when the line number is divided by 6, is 5. Set the value of opponent_score to the integer value of the line. Print opponent_score. Run the script and fix any errors you find.
Comment out all previous print statements. Add an else clause that sets the value of the variable ballpark to the line with the newline character removed. Print ballpark. Run the script and fix any errors you find.
Comment out all previous print statements. Change the code for the else clause so it prints the vales of date, home_team, home_score, opponent, opponent_score and ballpark. Run the script and fix any errors you find.
Create the function home_away. Replace the variable ballpark in the print statement with a call to home_away. Run the script and fix any errors you find.
Create the function win_loss. Add a call to this function inside the print statement. Run the script and fix any errors you find.
Rearrange the print statement so it's output looks like the format specified above. Run the script and fix any errors you find.
Testing
Your output should look like this
2011-07-02 Red Sox Away Astros Win 7 - 5 2011-07-03 Red Sox Away Astros Win 2 - 1 2011-07-04 Red Sox Home Blue Jays Loss 7 - 9 2011-07-05 Red Sox Home Blue Jays Win 3 - 2 2011-07-06 Red Sox Home Blue Jays Win 6 - 4 2011-07-07 Red Sox Home Orioles Win 10 - 4 2011-07-08 Red Sox Home Orioles Win 10 - 3 2011-07-09 Red Sox Home Orioles Win 4 - 0 2011-07-10 Red Sox Home Orioles Win 8 - 6 2011-07-15 Red Sox Away Rays Loss 6 - 9 2011-07-16 Red Sox Away Rays Win 9 - 5 2011-07-17 Red Sox Away Rays Win 1 - 0 2011-07-18 Red Sox Away Orioles Win 15 - 10 2011-07-19 Red Sox Away Orioles Loss 2 - 6 2011-07-20 Red Sox Away Orioles Win 4 - 0 2011-07-22 Red Sox Home Mariners Win 7 - 4 2011-07-23 Red Sox Home Mariners Win 3 - 1 2011-07-24 Red Sox Home Mariners Win 12 - 8 2011-07-25 Red Sox Home Royals Loss 1 - 3 2011-07-26 Red Sox Home Royals Win 13 - 9 2011-07-27 Red Sox Home Royals Win 12 - 5 2011-07-28 Red Sox Home Royals Loss 3 - 4 2011-07-29 Red Sox Away White Sox Loss 1 - 3 2011-07-30 Red Sox Away White Sox Win 10 - 2 2011-07-31 Red Sox Away White Sox Win 5 - 3
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
