So I already have everything done, however. I cannot figure out how to make a sorted dictionary out of the dictionary I have created. This dictionary should have the team that won the most games as 1st and so on.
This was the first dictionary I created img of dictionary
I have attempted
sorted_wins = sorted(matches_won[team].items(), key=lambda pair: pair[1], reverse=True)
but I keep getting met with an: AttributeError: 'int' object has no attribute 'items'
fp = open("C:/Users/Owner/Documents/WorldSeries.txt", "r")
matches = {}
matches_won = {}
year = 1903
for team in fp.readlines():
team = team.strip()
matches[year] = team
if team not in matches_won:
matches_won[team] = 0
matches_won[team]+=1
year += 1
fp.close()
sorted_wins = sorted(matches_won[team].items(), key=lambda pair: pair[1], reverse=True)
for team in matches_won:
print(team,":",matches_won[team])
again = "Y"
while again=="Y":
year = int(input("\n Enter a year in the range of 1903 through 2019: "))
if(year>=1903 and year<=2017):
print(matches[year], "team won match in",year)
print(matches_won[team])
else:
print("Invalid year")
again = input("\nWant to continue playing?(Y - yes, N - no): ").upper()
Trying to approaches that I've gotten I still only have this dictionary
This is the dictionary:
matches_won = {'Boston Americans' : 1,
'World Series Not Played in 1904' : 1,
'New York Giants' : 5,
'Chicago White Sox' : 3,
'Chicago Cubs' : 3,
'Pittsburgh Pirates' : 5,
'Philadelphia Athletics' : 5,
'Boston Red Sox' : 8,
'Boston Braves' : 1,
'Cincinnati Reds' : 5,
'Cleveland Indians' : 2,
'New York Yankees' : 27,
'Washington Senators' : 1,
'St. Louis Cardinals' : 11,
'Detroit Tigers' : 4,
'Brooklyn Dodgers' : 1,
'Milwaukee Braves' : 1,
'Los Angeles Dodgers' : 5,
'Baltimore Orioles' : 3,
'New York Mets' : 2,
'Oakland Athletics' : 4,
'Philadelphia Phillies' : 2,
'Kansas City Royals' : 2,
'Minnesota Twins' : 2,
'Toronto Blue Jays' : 2,
'World Series Not Played in 1994' : 1,
'Atlanta Braves' : 1,
'Florida Marlins' : 2,
'Arizona Diamondbacks' : 1,
'Anaheim Angels' : 1,
'San Francisco Giants' : 3,
'Houston Astros' : 1,
'Washington Nationals' : 1}