Answer to Question #177942 in Python for ankitha

Question #177942

Write a program that reads all the match outcomes and summarizes the information of all the matches.

Points are given to the teams based on the outcome of the match.

A win earns a team 3 points. A draw earns 1. A loss earns 0.

The following information is expected:

MP: Matches Played

W: Matches Won

D: Matches Drawn (Tied)

L: Matches Lost

P: Points


The team information should be displayed in descending order of points.Input


The first line contains a single integer N, denoting the total no. of matches played.

The following N lines contain outcomes of N matches.

Each of those lines has information on the teams (T1, T2) which played and the outcome (O) in format T1;T2;O.

The outcome (O) is one of 'win', 'loss', 'draw' and refers to the first team listed.

See Sample Input/Output for better understanding.

The team name may contain spaces.Output





1
Expert's answer
2021-04-03T19:59:35-0400
team = {}
matches = int(input('Enter the number of matches played: '))
# data input
for match in range(matches):
    data_match = input(f'Enter data for {match +1} match: ')
    team_1, team_2, outcome = data_match.split(';')
    if team_1 not in team:
        team[team_1] = [0,0,0,0,0]
    if team_2 not in team:
        team[team_2] = [0,0,0,0,0]
    team[team_1][0] +=1
    team[team_2][0] += 1
    if outcome == 'win':
        team[team_1][1] += 1
        team[team_1][4] += 3
        team[team_2][3] += 1
    elif outcome == 'loss':
        team[team_2][1] += 1
        team[team_2][4] += 3
        team[team_1][3] += 1 
    elif outcome == 'draw':
        team[team_1][2] += 1
        team[team_2][2] += 1
        team[team_1][4] += 1
        team[team_2][4] += 1
# data output
team_list=sorted(team.items(),key=lambda t:t[1][4],reverse=True) 
for item in team_list:
    print(f'Team: {item[0]}')
    print(f'MP: {item[1][0]}')
    print(f'W: {item[1][1]}')
    print(f'D: {item[1][2]}')
    print(f'L: {item[1][3]}')
    print(f'P: {item[1][4]}')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS