Question

Here is the red_sox.txt file 2011-07-02 Red Sox @ Astros Win 7-5 2011-07-03 Red Sox @...

Here is the red_sox.txt file

2011-07-02 Red Sox @ Astros Win 7-5
2011-07-03 Red Sox @ Astros Win 2-1
2011-07-04 Red Sox vs Blue Jays Loss 7-9
2011-07-05 Red Sox vs Blue Jays Win 3-2
2011-07-06 Red Sox vs Blue Jays Win 6-4
2011-07-07 Red Sox vs Orioles Win 10-4
2011-07-08 Red Sox vs Orioles Win 10-3
2011-07-09 Red Sox vs Orioles Win 4-0
2011-07-10 Red Sox vs Orioles Win 8-6
2011-07-15 Red Sox @ Rays Loss 6-9
2011-07-16 Red Sox @ Rays Win 9-5
2011-07-17 Red Sox @ Rays Win 1-0
2011-07-18 Red Sox @ Orioles Win 15-10
2011-07-19 Red Sox @ Orioles Loss 2-6
2011-07-20 Red Sox @ Orioles Win 4-0
2011-07-22 Red Sox vs Mariners Win 7-4
2011-07-23 Red Sox vs Mariners Win 3-1
2011-07-24 Red Sox vs Mariners Win 12-8
2011-07-25 Red Sox vs Royals Loss 1-3
2011-07-26 Red Sox vs Royals Win 13-9
2011-07-27 Red Sox vs Royals Win 12-5
2011-07-28 Red Sox vs Royals Loss 3-4
2011-07-29 Red Sox @ White Sox Loss 1-3
2011-07-30 Red Sox @ White Sox Win 10-2
2011-07-31 Red Sox @ White Sox Win 5-3

Use Python Please...

There is one deliverable for this assignment:

hw2.py

It must be in an hw2 directory, which you must create inside a hw directory inside you it117 directory.

Make sure the script obeys all the rules in the Script Requirements page.

To test this script you must copy into your hw2 directory the file red_sox.txt from /home/ghoffman/course_files/it117_files.

Specification

Create the function team_average which has the following header

def team_average(filename):

This function will return the average number of games won by the Red Sox from a text file with entries like this

2011-07-02      Red Sox @  Astros       Win 7-5
2011-07-03      Red Sox @  Astros       Win 2-1
2011-07-04      Red Sox vs Blue Jays    Loss 7-9

This function should create a file object for the file whose name is given by the parameter filename.

If the file cannot be opened, use a try/except statement to print an error message and don't do any further work on the file.

The function will count the number of lines and the number of games the Red Sox won and use these values to compute the average games won by the team.

This average should be expressed as an integer.

Your hw2.py file must contain the following test code at the bottom of the file

team_average('xxxxxxx')
print(team_average('red_sox.txt'))

Suggestions

Write this program in a step-by-step fashion using the technique of incremental development.

In other words, write a bit of code, test it, make whatever changes you need to get it working, and go on to the next step.

Write the function code to open the file for reading. This code should print an error message and exit the function if the file cannot be opened for reading.

Modify the function so it prints out the lines of the file.

Modify the function to use the split string method to create a list from the from the different fields in a line. Print this list.

For each line get the value of the won/lost field and print it. You will have to use a negative index number to get the value of this field. (Why?)

Count the number of lines in the file and return that number.

Count the number of wins and return that number.

Return the average as an integer percent.

Testing

When you run this script, you should see

Error: Unable to open xxxxxxx
76

Be sure to run this script on the Unix machine so you know it works in the environment in which I will run it when I score your homework.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

# Python program to print the average percent

#Screenshot of the code

Output:

# Do not copy the below code as there is a problem of indentation with the editor

def team_average(filename):

wins =0 ; # variable to count the wins

try:

with open(filename) as f: # opening the file

content = f.readlines() #reading the lines from file and storing it in list content

for line in content: # reading each line

line_list = line.split() # splitting the line to a list with default delimiter whitespace

if line_list[-2]== 'Win': #using negative index since the resukt win/loss is present in 2nd last index of the list

wins = wins + 1

total = len(content) # calculating total games played

percent = (wins*100)/total # calculating percent

return percent

except IOError: # if file doesn't exist

print "Error : Unable to open "+filename

print(team_average('red_sox.txt')); # testing the code

Add a comment
Know the answer?
Add Answer to:
Here is the red_sox.txt file 2011-07-02 Red Sox @ Astros Win 7-5 2011-07-03 Red Sox @...
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
  • 2011-07-02 Red Sox    7 Astros 5 Minute Maid Park 2011-07-03 Red Sox 2 Astros 1...

    2011-07-02 Red Sox    7 Astros 5 Minute Maid Park 2011-07-03 Red Sox 2 Astros 1 Minute Maid Park 2011-07-04 Red Sox 7 Blue Jays 9 Fenway Park 2011-07-05 Red Sox 3 Blue Jays 2 Fenway Park 2011-07-06 Red Sox 6 Blue Jays 4 Fenway Park 2011-07-07 Red Sox 10 Orioles 4 Fenway Park 2011-07-08 Red Sox 10 Orioles 3 Fenway Park 2011-07-09 Red Sox 4 Orioles 0 Fenway Park 2011-07-10 Red Sox 8 Orioles 6 Fenway Park 2011-07-15 Red...

  • PYTHON Text file Seriesworld attached below and it is a list of teams that won from...

    PYTHON Text file Seriesworld attached below and it is a list of teams that won from 1903-2018. First time mentioned won in 1903 and last team mentioned won in 2018. World series wasn’t played in 1904 and 1994 and the file has entries that shows it. Write a python program which will read this file and create 2 dictionaries. The first key of the dictionary should show name of the teams and each keys value that is associated is the...

  • 3) American League baseball teams play their games with the designated hitter rule, meaning that pitchers...

    3) American League baseball teams play their games with the designated hitter rule, meaning that pitchers do not bat. The league believes that replacing the pitcher, typically a weak hitter, with another player in the batting order produces more runs. Using a significance level of a = 0.05, determine if the average number of runs is higher for the American League Following are the average number of runs scored by each team in the 2016 season: American League National League...

  • Java : I keep getting something wrong in my loadfile method, how do I fix my...

    Java : I keep getting something wrong in my loadfile method, how do I fix my code? Thank you. here is what is contained in the text file (WorldSeriesWinners.txt) 112 1903 Boston Americans 1904 No World Series 1905 New York Giants 1906 Chicago White Sox 1907 Chicago Cubs 1908 Chicago Cubs 1909 Pittsburgh Pirates 1910 Philadelphia Athletics 1911 Philadelphia Athletics 1912 Boston Red Sox 1913 Philadelphia Athletics 1914 Boston Braves 1915 Boston Red Sox 1916 Boston Red Sox 1917 Chicago...

  • Using c 3 File Input & Data Processing Reading data from a file is often done in order to pro...

    using c 3 File Input & Data Processing Reading data from a file is often done in order to process and aggregate it to get ad- ditional results. In this activity you will read in data from a file containing win/loss data from the 2011 Major League Baseball season. Specifically, the file data/mlb_nl_2011.txt contains data about each National League team. Each line contains a team name fol- lowed by the number of wins and number of losses during the 2011...

  • Refer to the Lincolnville School District bus data. First, add a variable to change the type...

    Refer to the Lincolnville School District bus data. First, add a variable to change the type of engine (diesel or gasoline) to a qualitative variable. If the engine type is diesel, then set the qualitative variable to 0. If the engine type is gasoline, then set the qualitative variable to 1. Develop a regression equation using statistical software with maintenance cost as the dependent variable and age, odometer miles, miles since last maintenance, and engine type as the independent variables....

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