Python Forum
Getting a "Cannot be Opened"" error Message
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting a "Cannot be Opened"" error Message
#11
So, i was trying to read the score from the end. Instead of using  scores = game[-1]   i used  scores = game[1]. However i got a new error 

Error:
Traceback (most recent call last):   File "./hw2.py", line 25, in <module>     print(team_average('red_sox.txt'))   File "./hw2.py", line 20, in team_average     if (int(score[0]) - int(score[1])) > 0: IndexError: string index out of range
Reply
#12
And what do you get if you print out the values you are using for line 20??
Reply
#13
for score in scores_list:
   if (int(score[0]) - int(score[1])) > 0:
here you don't need the loop
for the first line the scores_list is ['7','5']
Reply
#14
So i finally got the code right and below is my final code :
def team_average(filename):
    numberOfGames = 0
    soxWins = 0
    try:
        file = open(filename, 'r')
    except:
        print (filename, "Cannot be opened")
    else:
        for line in file:
            numberOfGames += 1
            game = line.split()
            scores = game[-2]
            if scores == 'Win':
                soxWins += 1
        average_win = int((soxWins/numberOfGames) * 100)
        return average_win
team_average('xxxxxx')
print(team_average('red_sox.txt'))
I learned a lot just from this thread of course with your tremendous help. Below are some of the acquired knowledge:
  • Troubleshoot the code using the print statement
  • avoid the use of the exception any how and everywhere, it can hide errors.
  • indentation issue
  • Usage of the for loop
  • Etc...

Thanks a  lot gentlemen
Reply
#15
Good that you solved it.
Some points,average is a confusing name when you calculate percentage Confused 
Do not use bare except: and return out when error occurred.
Eg:
def team_average(filename):
   try:
       file = open(filename)
   except IOError as error:
       return error
   else:
       numberOfGames = 0
       soxWins = 0
       for line in file:
           numberOfGames += 1
           game = line.split()
           scores = game[-2]
           if scores == 'Win':
               soxWins += 1
       win_percent = int((soxWins/numberOfGames) * 100)
       return win_percent

print(team_average('red_sox.txt'))
Alternative way and a little more Pythonic could be:
with open('red_sox.txt') as file:
  all_games = [line for line in file]
  win = [game for game in all_games if 'Win' in game]
  win_percent = len(win) / len(all_games) * 100
  print(win_percent)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error message about iid from RandomizedSearchCV Visiting 2 933 Aug-17-2023, 07:53 PM
Last Post: Visiting
  does not save in other path than opened files before icode 3 833 Jun-23-2023, 07:25 PM
Last Post: snippsat
  Another Error message. the_jl_zone 2 943 Mar-06-2023, 10:23 PM
Last Post: the_jl_zone
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,682 Feb-09-2022, 09:55 AM
Last Post: ibreeden
Question How to get html information from a tab of my default browser opened with webbrowser? noahverner1995 2 4,336 Jan-14-2022, 10:02 AM
Last Post: noahverner1995
  understanding error message krlosbatist 1 1,856 Oct-24-2021, 08:34 PM
Last Post: Gribouillis
  Error message pybits 1 35,997 May-29-2021, 10:26 AM
Last Post: snippsat
  Rmarkdown opened by python code - errors Rav013 0 2,049 Apr-27-2021, 03:13 PM
Last Post: Rav013
  f-string error message not understood Skaperen 4 3,267 Mar-16-2021, 07:59 PM
Last Post: Skaperen
  Overwhelmed with error message using pandas drop() EmmaRaponi 1 2,300 Feb-18-2021, 07:31 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020