Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Script to enter sport team names and win/loss history
- # then to schedule a series of matches between pairs
- # Mike Kerry - Jan 2021 - Email: acclivity2@gmail.com
- import random
- # This is the function to get input from keyboard of Team Names, Wins and Losses
- def teams():
- no_of__teams = int(input("Enter number of teams"))
- mydic = {}
- for _ in range(no_of__teams):
- teamname = input("Team Name: ")
- win = int(input("No. of wins: ")) # this is dangerous ... script will crash on bad input
- lose = int(input("No. of losses: "))
- mydic[teamname] = [win, lose] # store team name and win/loss history in dictionary
- print(mydic)
- return mydic
- # This function picks one team at random from the global list "teamlist"
- # and removes the chosen name from the list so that the same team will not be picked again
- def pick_random_team():
- global teamlist
- namelist = random.choices(teamlist) # random.choices() returns a list
- name = namelist[0] # extract the one name
- teamlist.remove(name) # remove that team from the input list
- return name
- # Here I have commented out the function call to get input from keyboard
- # Instead (for testing purposes) I am using a pre-defined dictionary
- # main_dic = teams() # Normally this function would be called, rather than using a pre-defined dict.
- main_dic = {"Sussex": [5, 2], "Hants": [4, 3], "Kent": [6, 1], "Devon": [3, 4],
- "Bucks": [1, 6], "Oxon": [2, 5], "Herts": [3, 3]}
- print(main_dic)
- # Schedule some games
- # Make a loop to schedule pairs of teams to play. Repeat loop until all teams are scheduled to play
- # get a list of the team names in the dictionary
- teamlist = list(main_dic.keys())
- while len(teamlist) > 1: # While there are at least 2 teams still to be chosen ...
- team1 = pick_random_team() # Pick one team name at random
- team2 = pick_random_team() # Pick a 2nd team at random
- # Get the wins and losses history for these two teams, getting the data from the dictionary
- templist = main_dic[team1]
- team1wins = templist[0]
- team1loss = templist[1]
- templist = main_dic[team2]
- team2wins = templist[0]
- team2loss = templist[1]
- # Compute the win rates for each team in this pair
- team1winrate = team1wins * 100 / (team1wins + team1loss)
- team2winrate = team2wins * 100 / (team2wins + team2loss)
- # Compute the Winning Chance for each of these 2 teams
- team1winchance = team1winrate * 100 / (team1winrate + team2winrate)
- team2winchance = 100.0 - team1winchance
- # Print the schedule for this match
- print("Team Won Lost Win Rate Win Chance")
- print(team1.ljust(16), str(team1wins).ljust(10), str(team1loss).ljust(8),
- str(round(team1winrate, 2)).ljust(14), str(round(team1winchance, 2)).ljust(12))
- print(team2.ljust(16), str(team2wins).ljust(10), str(team2loss).ljust(8),
- str(round(team2winrate, 2)).ljust(14), str(round(team2winchance, 2)).ljust(12))
- print()
Add Comment
Please, Sign In to add comment