Feb-09-2017, 11:16 AM
(This post was last modified: Feb-09-2017, 11:16 AM by valerydolce.)
Hi Folks,
I'm having a "Cannot opened" error message even though i have the file in my directory. below is my code which consist of reading a file and determining the average game won.
Below is the content of 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
I'm having a "Cannot opened" error message even though i have the file in my directory. below is my code which consist of reading a file and determining the average game won.
Quote:#! /usr/bin/python3 def team_average(filename): numberOfGames = 0 soxWins = 0 try: file = open(filename, 'r') for line in file: numberOfGames += 1 game = line.split() scores = game[1] scores_list = scores.split('-') for score in scores_list: if int(score[0]) - int(score[1]) > 0: soxWins += 1 average_win = int(s0xWins/numberOfGames) * 100 except: print (filename, "Cannot be opened") team_average('xxxxx') print(team_average('red_sox.txt'))Quote:# This is my output (which is not what i was expecting )
xxxxx Cannot be opened
red_sox.txt Cannot be opened
None
Please what did i get wrong ?
Best,
valerydolce
Below is the content of 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