Question

Starting out with Python 4th edition I need help I followed a tutorial and have messed...

Starting out with Python 4th edition

I need help I followed a tutorial and have messed up also how does one include IOError and IndexError exception handling?

I'm getting errors: Traceback (most recent call last):
File, line 42, in <module>
main()
File , line 37, in main
winning_times = years_won(user_winning_team_name, winning_teams_list)
File , line 17, in years_won
for winning_team_Index in range(len(list_of_teams)):
TypeError: object of type '_io.TextIOWrapper' has no len()

Write program champions.py to solve problem 7.10. Include IOError and IndexError exception handling.

Output:

Enter the name of a team: Boston Red Sox

The Boston Red Sox won the world series 6 times between 1903 and 2009.

def world_series_list(file_name):
teams = open(file_name, 'r')
winning_teams_list = []

winning_team = teams.readline()

while winning_team != "":
winning_team.rstrip('\n')
winning_teams_list.append(winning_team)
winning_team = teams.readline()
return teams


def years_won(winning_team, list_of_teams):
winning_times = 0

for winning_team_Index in range(len(list_of_teams)):
if list_of_teams[winning_team_Index] == winning_team:
winning_times = winning_times + 1

return winning_times


def championships(winning_team_name, winning_times):
print(winning_team_name, "won the World Series", winning_times, 'times between 1903 and 2009')


def main():
file_name = 'WorldSeriesWinners.txt'

winning_teams_list = world_series_list(file_name)

print(winning_teams_list)

user_winning_team_name = input("Please enter team name: ")

winning_times = years_won(user_winning_team_name, winning_teams_list)

championships(user_winning_team_name, winning_times)


main()

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

def world_series_list(file_name):
try:
teams = open(file_name, 'r')
except:
print('File not Found')
return []
winning_teams_list = []
winning_team = teams.readline()
while winning_team != "":
winning_team.rstrip('\n')
winning_teams_list.append(winning_team)
winning_team = teams.readline()
return winning_teams_list #The error was in this line you were returning teams instead of list object


def years_won(winning_team, list_of_teams):
winning_times = 0
for winning_team_Index in range(len(list_of_teams)):
if list_of_teams[winning_team_Index] == winning_team:
winning_times = winning_times + 1
return winning_times


def championships(winning_team_name, winning_times):
print(winning_team_name, "won the World Series", winning_times, 'times between 1903 and 2009')


def main():
file_name = 'WorldSeriesWinners.txt'
winning_teams_list = world_series_list(file_name)
print(winning_teams_list)
user_winning_team_name = input("Please enter team name: ")
winning_times = years_won(user_winning_team_name, winning_teams_list)
championships(user_winning_team_name, winning_times)

main()

The code snippet screenshot

Only change is in the first function rest is same

Add a comment
Know the answer?
Add Answer to:
Starting out with Python 4th edition I need help I followed a tutorial and have messed...
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
  • Chapter 7, Problem 5PP in Starting out with Visual C# (4th Edition) World Series Champions Teams.txt...

    Chapter 7, Problem 5PP in Starting out with Visual C# (4th Edition) World Series Champions Teams.txt - This ile contains a list of several Major League baseball teams in alphabetical order. Each team listed in the file has one the World Series at least once. WorldSeriesWinners.txt - This file contains a chronological list of the World Series' winning teams from 1903 through 2012. Create an application that displays the contents of the Teams.txt file in a ListBox control. When the...

  • 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...

  • 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...

  • Hello I need help with python programming here is my code # Trivia Challenge # Trivia...

    Hello I need help with python programming here is my code # Trivia Challenge # Trivia game that reads a plain text file import sys def open_file(file_name, mode): """Open a file.""" try: the_file = open(file_name, mode) except IOError as e: print("Unable to open the file", file_name, "Ending program.\n", e) input("\n\nPress the enter key to exit.") sys.exit() else: return the_file def next_line(the_file): """Return next line from the trivia file, formatted.""" line = the_file.readline() line = line.replace("/", "\n") return line def next_block(the_file):...

  • I need help in Python. This is a two step problem So I have the code...

    I need help in Python. This is a two step problem So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:) 7. Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file...

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