In Python, Program description: In the module is the file WorldSeriesWinners.txt
ID: 3689472 • Letter: I
Question
In Python,
Program description:
In the module is the file 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)
Write a program that lets the user enter the name of a team and then displays the number of times that team has won the World Series in the time period from 1903 through 2009.
The program is required to use a list to read in the data and for in loop for counting the times won
Explanation / Answer
def main():
infile = open('WorldSeriesWinners.txt', 'r')
for line in open('WorldSeriesWinners.txt'):
line = line.rstrip(' ')
winners = infile.read().splitlines()
infile.close()
team = input('PLease enter the name of a team: ')
counter = 0
for winner in winners:
if team.lower() == winner.lower():
counter = counter + 1
if counter == 1:
print("The", team, "won the world series", counter, "time between 1903 and 2009.")
elif counter > 1:
print("The", team, "won the world series", counter, "times between 1903 and 2009.")
else:
print("The", team, "never won the world series.")
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.