acclivity

pySportTeams

Jan 3rd, 2021 (edited)
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. # Script to enter sport team names and win/loss history
  2. # then to schedule a series of matches between pairs
  3.  
  4. # Mike Kerry - Jan 2021 - Email: acclivity2@gmail.com
  5.  
  6. import random
  7.  
  8.  
  9. # This is the function to get input from keyboard of Team Names, Wins and Losses
  10. def teams():
  11.     no_of__teams = int(input("Enter number of teams"))
  12.     mydic = {}
  13.     for _ in range(no_of__teams):
  14.         teamname = input("Team Name: ")
  15.         win = int(input("No. of wins: "))   # this is dangerous ... script will crash on bad input
  16.         lose = int(input("No. of losses: "))
  17.         mydic[teamname] = [win, lose]        # store team name and win/loss history in dictionary
  18.  
  19.     print(mydic)
  20.     return mydic
  21.  
  22.  
  23. # This function picks one team at random from the global list "teamlist"
  24. # and removes the chosen name from the list so that the same team will not be picked again
  25. def pick_random_team():
  26.     global teamlist
  27.     namelist = random.choices(teamlist)         # random.choices() returns a list
  28.     name = namelist[0]                          # extract the one name
  29.     teamlist.remove(name)                       # remove that team from the input list
  30.     return name
  31.  
  32.  
  33. # Here I have commented out the function call to get input from keyboard
  34. # Instead (for testing purposes) I am using a pre-defined dictionary
  35.  
  36. # main_dic = teams()        # Normally this function would be called, rather than using a pre-defined dict.
  37.  
  38. main_dic = {"Sussex": [5, 2], "Hants": [4, 3], "Kent": [6, 1], "Devon": [3, 4],
  39.             "Bucks": [1, 6], "Oxon": [2, 5], "Herts": [3, 3]}
  40.  
  41. print(main_dic)
  42.  
  43. # Schedule some games
  44. # Make a loop to schedule pairs of teams to play. Repeat loop until all teams are scheduled to play
  45.  
  46. # get a list of the team names in the dictionary
  47. teamlist = list(main_dic.keys())
  48.  
  49. while len(teamlist) > 1:        # While there are at least 2 teams still to be chosen ...
  50.  
  51.     team1 = pick_random_team()      # Pick one team name at random
  52.     team2 = pick_random_team()      # Pick a 2nd team at random
  53.  
  54.     # Get the wins and losses history for these two teams, getting the data from the dictionary
  55.     templist = main_dic[team1]
  56.     team1wins = templist[0]
  57.     team1loss = templist[1]
  58.  
  59.     templist = main_dic[team2]
  60.     team2wins = templist[0]
  61.     team2loss = templist[1]
  62.  
  63.     # Compute the win rates for each team in this pair
  64.     team1winrate = team1wins * 100 / (team1wins + team1loss)
  65.     team2winrate = team2wins * 100 / (team2wins + team2loss)
  66.  
  67.     # Compute the Winning Chance for each of these 2 teams
  68.     team1winchance = team1winrate * 100 / (team1winrate + team2winrate)
  69.     team2winchance = 100.0 - team1winchance
  70.  
  71.     # Print the schedule for this match
  72.     print("Team             Won        Lost     Win Rate       Win Chance")
  73.     print(team1.ljust(16), str(team1wins).ljust(10), str(team1loss).ljust(8),
  74.           str(round(team1winrate, 2)).ljust(14), str(round(team1winchance, 2)).ljust(12))
  75.     print(team2.ljust(16), str(team2wins).ljust(10), str(team2loss).ljust(8),
  76.           str(round(team2winrate, 2)).ljust(14), str(round(team2winchance, 2)).ljust(12))
  77.     print()
  78.  
Add Comment
Please, Sign In to add comment