Question
Python Programming Code Help Needed: Baseball The file ALE.txt contains the information shown in table 5.6. Write a program to use the file to produce
Python Programming Code Help Needed:
Baseball The file ALE.txt contains the information shown in table 5.6. Write a program to use the file to produce a text file containing the information in table 5.7. In the new file, the baseball teams should be in descending order by the percentage of games won.
with open("ALE.txt",'r') as rfile:
lines = rfile.read().split(' ')
while '' in lines:
lines.remove('')
TeamData = dict()
for line in lines:
data = line.split(' ')
#team, won, lost = line.split(' ')
won, lost, team = data[-2], data[-1], " ".join(data[0:-2])
team, won, lost = team.strip(), int(won), int(lost)
TeamData[team] = [won, lost]
percentData = dict()
teams = TeamData.keys()
for team in teams:
won, lost = TeamData[team]
percent = won/(won+lost)
percentData[team] = percent
percents = [x for x in percentData.values()]
percents.sort(reverse = True)
# Write the data wit standings in descending order to the file ALE.txts
with open("ALE.txt",'w') as wfile:
for i in range(len(percents)):
for k,v in percentData.items():
if v == percents[i]:
won, lost = TeamData[k]
writer = "{} {} {} {:.3f} ".format(k, won, lost, v)
wfile.write(writer)
Here is my issue:
Traceback (most recent call last): File "/home/poulako5/hello.py", line 17, inTABLE 5.6 American League East games won and lost in 2014. Team Baltimore Boston New York Tampa Bay Toronto TABLE 5.7 Final 2014 American League East standings. Team Pct Baltimore 0.593 New York 0.519 Toronto 0.512 Tampa Bay 0.475 Boston 0.438 Read Aloudwon, lost, team = data[-2], data[-1], " ".join(data[0:-2]) IndexError: list index out of range
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started