Team Points in Python

Problem Statement:

This is IPL and matches are going on. You are a programmer who writes programs to output summarized charts of matches already played.
You will be given some input and you need to print a summarized chart in descending order of points.

Points criteria->
Match won-> 2
Draw-> 1
Loss-> 0

For example
Input:
4
RR-RCB-loss
MI-KKR-loss
CSK-RR-win
GT-RCB-draw
Output:
Team: RR | Total Matches: 2 | Won: 0, Lost: 2, Draw: 0, Points: 0
Team: RCB | Total Matches: 2 | Won: 1, Lost: 0, Draw: 1, Points: 3
Team: MI | Total Matches: 1 | Won: 0, Lost: 1, Draw: 0, Points: 0
Team: KKR | Total Matches: 1 | Won: 1, Lost: 0, Draw: 0, Points: 2
Team: CSK | Total Matches: 1 | Won: 1, Lost: 0, Draw: 0, Points: 2
Team: GT | Total Matches: 1 | Won: 0, Lost: 0, Draw: 1, Points: 1

Code to find Team Points in Python:

''' Add teams to dictionary '''
def add_teams(team,result,teams):
    #if team already exists in list, update the list
    if team in teams:
        points = teams[team]
    #else set to 0
    else:
        points = [0,0,0]

    #points[win,loss,draw]
    result = result.lower()
    if(result=="won"):
        points[0] +=1
    elif(result=="loss"):
        points[1] +=1
    else:
        points[2] +=1
    
    #update teams
    teams[team] = points


''' calculate result to be displayed '''
def calc_result(teams):
    list_res= list()

    for team in teams:
        won,lost,draw = teams[team]

        #calculate points
        points = 2*won + draw
        
        #total matches played
        total = won+lost+draw

        #string to be displayed
        string = f"Team: {team} | Total Matches: {total} | Won: {won}, Lost: {lost},  Draw: {draw}, Points: {points}"
        list_res.append(string)

    return list_res
    
''' main function '''
n=int(input("Number of Matches Played: "))
teams=dict()

for i in range(n):
    ip = input().split("-")
    # ip[0]: team played | ip[1]: against team | ip[2]: result

    #for team1
    add_teams(ip[0],ip[2],teams)

    #for team2, reverse the result
    if(ip[2]=="win"):
        add_teams(ip[1],"loss",teams)
    elif(ip[2]=="loss"):
        add_teams(ip[1],"win",teams)
    else:
        add_teams(ip[1],"draw",teams)

'''result to display'''
res = calc_result(teams)

'''display the results'''
for string in res:
    print(string)

Output:

Output to find Team Points in Python

Also Read:

Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you