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()
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
Starting out with Python 4th edition I need help I followed a tutorial and have messed...
Python You will download a file by right clicking on the file name then chose Save link As: WorldSeriesWinners.txt . This file contains a chronological list of the World Series winning teams from 1903 through 2009. (The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2009. Note that the World Series was not played in 1904 or 1994.) Write a...
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 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 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...
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...